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>
Related
I am getting below error, When i try to Assign the Appshell to MainPage
System.ObjectDisposedException: 'Cannot access a disposed object.
Object name: 'Xamarin.Forms.Platform.Android.ShellFlyoutRenderer'.'
Code:
Application.Current.MainPage = new AppShell();
Initially, I had SpleshScreen as MainPage, Then I redirected to Google OAuth to get Auth token.
Once I get the Token, I try to Assign AppShell to the main page but get the Error.
FIX 1:
Perhaps you've already created an AppShell. If so, remove the code or xaml that created it the first time. Two places to look:
In code. Is there anywhere else that already says new AppShell();? If so, remove that (or comment it out).
In App.xaml. Does it look something like this:
<Application ...>
<Application.Resources>
...
</Application.Resources>
<!-- This section creates an app Shell -->
<Shell ...>
...
</Shell>
</Application>
If so, the entire <Shell> ... </Shell> needs to be in its own xaml file, AppShell.xaml.
FIX #2:
Perhaps you create AppShell at a place in code where Xamarin is not ready to make a UI change. Wrap it in BeginInvoke...:
Device.BeginInvokeOnMainThread(() =>
{
Application.Current.MainPage = new AppShell();
});
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
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()
}
}
I am using a PCL project. In this I am loading a url in a Xamarin forms webview container. This url has a button with a telephone number in it. When I load it in the same url in the phone's chrome browser, it brings up the dialer, however, when I load the url in a Xamarin.Forms.WebView control I get an error net::ERR_UNKNOWN_URL_SCHEME
This is my code in the xaml page. This is a Xamarin.Forms.Webview control here.
<WebView x:Name="wvCntr" Source="{Binding TheWebUrl, Mode=TwoWay }"
WidthRequest="1000" HeightRequest="1000" />
Then in my Xaml.cs page, I have this code
public class WContainer : WebViewClient
{
public override bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, string url)
{
//What code here?
return true;
}
}
How is supposed to work? The Xaml page has Xamarin.Forms.Webview and the code behind I am using an Android.Webkit.Webview. I am not able to make the connect here and then get the code which actually pulls up the dialer. I am using Xamarin in Visual Studio 2015.
Any help or code appreciated.
I've started working on an Alloy & Titanium project, and i need to create a new window with a new view. I've already created the files, and created the controller and view in my index.js file.
function onListClick(e) {
var win = Alloy.createController('controlPage').getView();
win.open();
}
This is the code that is supposed to open the new View, but...
Uncaught TypeError: Object View has no method "open"
I've immediately checked the online reference, and there actually is no module "open" inside the Ti.UI.View object.
What am i supposed to do? controlPage.xml (and the relative .js and .tss file) do exist.
Well,
is controlPage.xml:
<Alloy>
<Window>
</Window>
</Alloy>
If it is, then you can win.open() since window objects have an open() method
If however, you actually have a view as your title, the references to Ti.UI.View, etc indicates, then you would need to add your view to a parent container.