Can not Access the disposed object( Xamarin forms (5.0) - xamarin

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();
});

Related

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

Titanium & Alloy - Opening a new View fails?

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.

How to clear UWP WebView cache?

I am using WebView in my UWP application and I want to clear the cache while closing the app, is there a way? I know I can disable cache by adding headers into my HttpRequestMessage as mentioned in this link. However, I want to be able to clear the cache upon app exit.
I did try WebView.ClearTemporaryWebDataAsync() without any success.
Once something is cached it normally remains throughout the app.
Any help is appreciated, thanks.
Edit : Adding code snippet
var webView = new WebView();
webView.Navigate(new Uri("http://refreshyourcache.com/en/cache-test/"));
await WebView.ClearTemporaryWebDataAsync(); //static method
webView.Navigate(new Uri("http://refreshyourcache.com/en/cache-test/"));
I expect the static method to clear cache and when I navigate to same page again its cache should be cleared. Am I doing something wrong here?
In UWP (XAML) there is the ClearTemporaryWebDataAsync method, that allows to webview's cache and IndexedDB data. And there is similar method for JavaScript in MSApp - clearTemporaryWebDataAsync.
Here is code sample (based on your one) that works for me:
XAML:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel>
<WebView x:Name="webView" Width="800" Height="600"></WebView>
<Button x:Name="refreshBtn" Content="Refresh" ></Button>
</StackPanel>
</Grid>
C#:
public MainPage()
{
this.InitializeComponent();
refreshBtn.Tapped += RefreshBtn_Tapped;
webView.Navigate(new Uri("http://refreshyourcache.com/en/cache-test/"));
}
private async void RefreshBtn_Tapped(object sender, TappedRoutedEventArgs e)
{
await Windows.UI.Xaml.Controls.WebView.ClearTemporaryWebDataAsync();
webView.Navigate(new Uri("http://refreshyourcache.com/en/cache-test/"));
}
When I click refresh button, cache is cleared -- I see green image.
There was no possibility to do it in 8.1 according
Ten Things You Need to Know About WebView – An Update for Windows 8.1
the closest method you can use now is WebView.Refresh that "reloads the file without forced cache validation by sending a "Pragma:no-cache" header to the server."
... or just clear cache in explorer

How to pass data when calling another Windows-Phone App?

I am starting in WP development but I already got the following situation.
I have my app and I need other apps to call it passing data/parameters that I will specify.
For example:
App X will call and open my app, passing parameters like id, name and age.
Is there any way to do it?
Define a URI association for your app.
To do so add content like this to the app's manifest file:
<Extensions>
<Protocol Name="myapp" NavUriFragment="encodedLaunchUri=%s" TaskID="_default" />
</Extensions>
Define a class that derives from UriMapperBase to handle incoming URIs and register right after the call to RootFrame.Navigated + = /*...*/ by doing this:
RootFrame.UriMapper = new MyPersonalUriMapper();
Other apps can open your app by doing something like this:
Windows.System.Launcher.LaunchUriAsync(new System.Uri("myapp:4711/Luciano/24"));
You can parse this URI in your UriMapper to open the page you wanted with ID=4711, Name=Luciano and Age=24.

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