MVVMCross side menu with mixed forms/native views - xamarin

Is there a way to have a side menu on iOS (navigation Drawer) on Android that uses a native view for the menu, but uses Xamarin.Forms pages for the content?
The Forms view presenter doesn't seem to support this setup.
The template that I'm using embeds the fragment in a frame on Android.
[MvxFragmentPresentation(typeof(MainContainerViewModel), Resource.Id.content_frame)]
public class MainFragment : BaseFragment<MainViewModel>
And on iOS it uses this hint.
[MvxSidebarPresentation(MvxPanelEnum.Center, MvxPanelHintType.ResetRoot, false)]
public class MainViewController : BaseViewController<MainViewModel>
I'm trying to do something like this with a ContentPage instead.
[MvxContentPagePresentationAttribute(AndroidFrameId = "content_frame", iOSPanel=MvxPanelEnum.Center)]
public partial class MainPage : MvxContentPage<MainViewModel>

Related

How can I implement SfMap in Xamarin

How can I implement SfMaps in my project which is based on MVVM structure? 1st I have to get my current location and show it on the map then I have to drag that marker point anywhere and set it as the current location
Add SfMaps to your project from NuGet. In android no need for native configurations, but to launch the SfMaps in iOS, call the SfMapsRenderer.Init() in the FinishedLaunching overridden method of the AppDelegate class
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
…
global::Xamarin.Forms.Forms.Init ();
Syncfusion.SfMaps.XForms.iOS.SfMapsRenderer.Init();
LoadApplication (new App ());
…
}
Add the following namespace in your view file.
xmlns:maps="clr-namespace:Syncfusion.SfMaps.XForms;assembly=Syncfusion.SfMaps.XForms"
And add the below code to initializing map in view
<maps:SfMaps >
</maps:SfMaps>
this is a very basic way to add SFMap in Xamarin. forms for more details and help
please visit this link: https://help.syncfusion.com/xamarin/maps/getting-started?cs-save-lang=1&cs-lang=xaml

How to create a Rich Text box with or without web view in xamarin forms?

I want create a Rich Text box with or without web view in xamarin forms. I tried many examples but none of that worked for me. The examples are
https://github.com/XAM-Consulting/TEditor
The problem with this example was, it is too slow for me and it was not editing my text at all. And it was getting crash numerous times. So I tried to follow this example
https://forums.xamarin.com/discussion/95318/rich-text-box-in-xamarin-forms
but the code by Adam.Else gave me lots of bugs and it was not working. I have reported the bug in stackoverflow. As you can see this link below-
Unhandled Exception: Xamarin.Forms.Xaml.XamlParseException: Position 12:21. StaticResource not found for key FromRTFConverter occurred
I don't know how to fix this issue. Any suggestions?
I did it using web view. First I downloaded a Rich text box which was created using HTML/CSS and JavaScript. This is the link for the Rich text box which I'm going to use as the web view in my xamarin forms app.
MainPage.xaml
<StackLayout>
<!-- Place new controls here -->
<WebView x:Name="MyWebView" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" />
</StackLayout>
MainPage.xaml.cs
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
MyWebView.Source = new HtmlWebViewSource();
MyWebView.Source = DependencyService.Get<IBaseUrl>().Get();
}
}
I created a interface called IBaseUrl. This is my code
public interface IBaseUrl { string Get(); }
And I created a class in Native called BaseUrl_Android for calling the Html file which I have put in my Assets folder in android. This is my code for BaseUrl_Android
[assembly: Dependency(typeof(BaseUrl_Android))]
namespace webviewdemo.Droid
{
public class BaseUrl_Android : IBaseUrl
{
public string Get()
{
return "file:///android_asset/fontawesome5.html";
}
}
}
Add your HTML and JavaScript file in assets folder. If you have any doubt in adding and using html file in xamarin forms refer this link. This is all you have to do to get an awesome Rich text box in Xamarin Forms.
This is the screenshot of my Rich text box
Is there any way to get the htlm code of the textarea like the java script does in c# or directly by typing a new code in the html file. capture of the html code retrieved by the javascript function
I tried to retrieve the html code by using a simple implementation in the html file and in the C# file by using the following code:
await webview.EvaluateJavaScriptAsync($"document.getElementById('theText').value;");
Unfortunately, the html code is totally different and the

Xamarin.Forms How to switch pages using MVVMLight

I'm currently working on a Xamarin.forms project using .NET Standard as code sharing strategy. I try to use the MVVM pattern by using the MvvmLightLibsStd10 library. I already successfully setup the MVVM structure by using this tutorial:
https://www.linkedin.com/pulse/build-xamarinforms-net-standard-mvvm-light-app-rafael-carvalho
I can't use Navigation.PushAsync(new Page()); because it only works in code behind and not in the ViewModel.
I already tried to Pass Navigation trough the VM constructor, like describe over here:
Xamarin.form Page Navigation in mvvm
But when I try this method, an error occurred at "LoadApplication(new DemoMVVM2.App());" in MainPage.
How can I switch pages using MVVM Xamarin.Forms with MVVMLight (based on the code from my first url)?
but I have no Idea how I can switch Pages via the ViewModel and keeping the header with back button.
Generally when working with MVVMLight you'll be using a NavigationService.
This class can be constructor injected in your VM, thanks to the build in IOC in MVVMLight.
With it you can do Navigate and GoBack in your VM, triggering a real navigation on the current stack.
Only thing that you maybe missed, is the fact that you need to write one yourself for Xamarin forms.
But Laurent Bugnion ( the owner of MVVMLight ) supplied an example available here:
https://github.com/lbugnion/sample-2016-vslive-crossplatform/blob/master/Flowers/Flowers.Forms/Flowers.Forms/Helpers/NavigationService.cs
You can pass a callback to your ViewModel(VM) and on Command or whatever action call your navigation code which is in your page (View). this way you can keep your navigation code in your page and your binding logic in your ViewModel.
interface NavHandler{
void navigateToSomeView();
}
public class MyPage : ContentPage,NavHandler{
public MyPage(){
BindingContext = new MyViewModel(this);
}
void navigateToSomeView(){
Navigation.PushAsync(new Page2());
}
}
public class MyViewModel{
NavHandler handler;
public MyViewModel(NavHandler handler){
this.handler = handler
}
//Your action
this.btnClicked = new Command(async()=>{
handler.navigateToSomeView()
}
}

Custom video page for xamarin ios

I create some project that consists from videoplayer. For Xamarin android I implemented through messaging center.
On android:
Page(xamarin forms) -> (click button and sending message to MainActivity) -> calling MyVideoActivity via intent
With android all works perfectly.
But I am newbie with ios and xamarin ios also.
And I don't know is this real to implement the same in xamarin ios through messaging center ? Also I would like use .xib file(with ui, taken from Xcode). I am not sure how to implement it. I would like to do it through messaging center -> calling custom ViewController with my .xib file. Is it real?
p.s. I've found some example with PageRenderer, but don't investigate it. I am not sure that it will be suitable for me.
Call the following methods in your MessagingCenter.Subscribe lambda:
XIB-based ViewController:
var vc = new XIBBasedViewController();
// Set any public properties / call methods on your `XIBBasedViewController`
InvokeOnMainThread(() => UIApplication.SharedApplication.Windows[0].RootViewController.PresentViewController(vc, true, null));
Storyboard-based ViewController:
var storyboard = UIStoryboard.FromName("StoryBoardFileNameMinusExtension", null);
var vc = storyboard.InstantiateViewController("AStoryBoardIDAssignedToYourViewController") as AViewControllerSubClass;
// Set any public properties / call methods on your `AViewControllerSubClass`
InvokeOnMainThread(() => UIApplication.SharedApplication.Windows[0].RootViewController.PresentViewController(vc, true, null));
Note: A good place to setup your messaging center subscribes is in the FinishedLaunching of the AppDelegate class.

How to use xaml with viewmodel in another project

I have a WP7application app containing a xaml file called page1.xaml with a viewmodel . The xaml file contains a button with a binding command
I have another project called sampleapplication in which i am launching an emulator and have to display the above page1.xaml file which is in another project.
I am able to load the above xaml file from wp7 app in the current project using
(Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/WP7application ;component/Views/page1.xaml", UriKind.Relative));
but i am unable to worked with the events after the xaml is loaded. How can i get the button worked in my current project ?
i have added all the references to the wp7 app view and viewmodels in my current sampleapplication
You can easily have the view and view model in separate assemblies but the assembly (app or library) which has the view in it must have a reference to the library that the view-model is in.
There are two things to be aware of though:
1. If you're using different assemblies for the view and view-model the view-model must be in a class library and not the main application.
2. Be sure to structure your code so that you don't have any circular references. (This can require discipline to avoid as complexity grows.)
It sounds as though the DataContext of your view is not set to an instance of the view model. There are several ways you can do this.
The easiest is simply to put the following code in the Loaded event of the view:
private void PhoneApplicationPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
DataContext = new ViewModel();
}
The preferred way is to define a view model locator in your application project. Create an instance of your view model.
public class ViewModelLocator
{
private readonly ViewModel _viewModel = new ViewModel();
public ViewModel Main
{
get { return _viewModel; }
}
}
Create your view model locator in the App.XAML:
<Application xmlns:vm="clr-namespace:groovd.client.phone.ViewModels" >
<Application.Resources>
<ResourceDictionary>
<vm:ViewModelLocator xmlns:vm="clr-namespace:MyApp.ViewModels" x:Key="Locator" />
</ResourceDictionary>
</Application.Resources>
</Application>
Then get the property from the view model locator in the page:
<phone:PhoneApplicationPage
DataContext="{Binding Main, Source={StaticResource Locator}}">
</phone:PhoneApplicationPage>

Resources