Override Manifest file using xamarin android - xamarin

Is there any possibility of the overriding manifest with a view to set at run time after the user logs in?

You could add it in your App.xaml.cs.
[Application(UsesCleartextTraffic =true)]
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new Page2());
}
}

Related

Accessing ViewModel properties from Viewclass

I want to call Content page class from Android .cs Class.
I use StartActivity(new Intent(Application.Context, typeof(LoginPage)));
showing error message
"System.ArgumentException: type Parameter name: Type is not derived
from a java type "
Does anyone know the solution for this? Please help me in Xamarin.Forms.
This should be you App.xaml.cs class
public App()
{
MainPage = new NavigationPage ( new MainPage());
}
MainActivity.cs file
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
private TextView setting;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
SetContentView(Resource.Layout.HomeScreen);
setting = FindViewById<TextView>(Resource.Id.settingText);
//check setting button is null or have value
setting.Click += delegate
{
Xamarin.Forms.Application.Current.MainPage.Navigation.PushAsync(new LoginPage());
};
}
}
Use code above & debug to check if setting button have instance or is null.
Replace typeof(LoginPage) with LoginPage.Class or LoginPage::Class.java (kotlin)

Cant find page in Xamarin.forms

i have some trouble finding the page that i wanna navigate to in Xamarin.forms. I've been looking everywhere and it seems i'm the only one experiencing the issue.
I have these 3 files:
And yet i keep getting an error on the new HomePage (could not be found). The class is right there, it just cant find it..
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
void LoginButton_Clicked(object sender, EventArgs e)
{
bool isIdEmpty = String.IsNullOrEmpty(idEntry.Text);
bool isPasswordEmpty = String.IsNullOrEmpty(passwordEntry.Text);
if(isIdEmpty || isPasswordEmpty)
{
} else
{
Navigation.PushAsync(new HomePage());
}
}
}
}
Check to see if both pages are in the same namespace. For instance, if MainPage is in the YourApp namespace:
namespace YourApp
{
public class MainPage
{
// ... Your code
}
}
and the HomePage is in the YourApp.Pages namespace, then you need to add a using statement to the top of your MainPage:
using YourApp.Pages;
// Probably other ones here
namespace YourApp
{
public class MainPage
{
// ... Your code
}
}
Or, you can specify the full namespace in your declaration: Navigation.PushAsync(new YourApp.Pages.HomePage());

Xamarin Android Share Link/Text via social media from custom renderer

I wan't to share a link via social media from custom renderer
public class CustomActions : ICustomActions
{
Context context = Android.App.Application.Context;
public void ShareThisLink()
{
Intent sharingInt = new Intent(Android.Content.Intent.ActionSend);
sharingInt.SetType("text/plain");
string shareBody = "https://www.google.com";
sharingInt.PutExtra(Android.Content.Intent.ExtraSubject, "Subject");
sharingInt.PutExtra(Android.Content.Intent.ExtraText, shareBody);
context.StartActivity(Intent.CreateChooser(sharingInt, "Share via"));
}
}
This error occur
Android.Util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
even when I added the below code I still get same error
sharingInt.AddFlags(ActivityFlags.NewTask);
The problem is that Intent.CreateChooser creates yet another Intent. What you want to do is to set the flag on this new intent:
public void ShareThisLink()
{
Intent sharingInt = new Intent(Android.Content.Intent.ActionSend);
sharingInt.SetType("text/plain");
string shareBody = "https://www.google.com";
sharingInt.PutExtra(Android.Content.Intent.ExtraSubject, "Subject");
sharingInt.PutExtra(Android.Content.Intent.ExtraText, shareBody);
var intent = Intent.CreateChooser(sharingInt, "Share via");
intent.AddFlags(ActivityFlags.NewTask);
context.StartActivity(intent);
}
Alternatively to avoid the need to do this, you could cache the MainActivity instance Xamarin.Forms uses:
public MainActivity
{
public static MainActivity Instance {get;private set;}
protected override void OnCreate(Bundle bundle)
{
Instance = this;
...
}
}
And then use the Instance as the Context in your code instead of the Application.Context

MVVMCross 5.3.2 UWP: Where to Get IMvxWindowsFrame for MvxFormsUwpViewPresenter

I'm working out of the Xamarin Forms for MVVMCross 5 Solution Template and updated the packages to the latest version (5.3.2 for MVVMCross). Doing that changes some namespaces around particularly in the UWP project.
It seems that I need to resolve IMvxViewPresenter as MvxFormsUwpViewPresenter which takes a IMvxWindowsFrame as an argument. In the setup file method of Setup.cs there's a XamlControls.Frame rootFrame passed as an argument but I'm not sure if that's suppose to be cast somehow as IMvxWindowsFrame.
Where can you pull the object that implements IMvxWindowsFrame from or is there another way to turn the rootFrame into an IMvxWindowsFrame legitimately.
public class Setup : MvxFormsWindowsSetup
{
private readonly LaunchActivatedEventArgs _launchActivatedEventArgs;
public Setup(XamlControls.Frame rootFrame, LaunchActivatedEventArgs e) : base(rootFrame, e)
{
_launchActivatedEventArgs = e;
// Mvx.RegisterSingleton<IMvxWindowsFrame>(rootFrame);
}
protected override void InitializeFirstChance()
{
base.InitializeFirstChance();
Mvx.RegisterSingleton<Core.Services.ILocalizeService>(new Services.LocalizeService());
Mvx.RegisterSingleton<ISettings>(CrossSettings.Current);
Mvx.RegisterType<IMvxViewPresenter, MvxFormsUwpViewPresenter>();
}
protected override MvxFormsApplication CreateFormsApplication()
{
return new Core.FormsApp();
}
protected override IMvxApplication CreateApp()
{
return new Core.MvxApp();
}
protected override IMvxTrace CreateDebugTrace()
{
return new Core.DebugTrace();
}
}
public sealed partial class MainPage : WindowsPage
{
public MainPage()
{
this.InitializeComponent();
var start = Mvx.Resolve<IMvxAppStart>();
start.Start();
var presenter = Mvx.Resolve<IMvxViewPresenter>() as MvxFormsUwpViewPresenter;
LoadApplication(presenter.FormsApplication);
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}
}
EDIT: I've been looking more into the class MvxFormsWindowsSetup in the source code at https://github.com/MvvmCross/MvvmCross/blob/develop/MvvmCross-Forms/MvvmCross.Forms.Uwp/Platform/MvxFormsWindowsSetup.cs. It appears that in the method CreateViewPresenter that the IMvxViewPresenter is registered as a singleton with the MvxWrappedFrame already inside but by default the code does not resolve when calling var presenter = Mvx.Resolve() as MvxFormsUwpViewPresenter; in the windows page. Possible bug? Trying to see if I can resolve it myself.
Looks like it fails to resolve even if I put the code right after when Mvx is suppose to register the type / singleton
protected override IMvxWindowsViewPresenter CreateViewPresenter(IMvxWindowsFrame rootFrame)
{
var presenter = new MvxFormsUwpViewPresenter(rootFrame, FormsApplication);
Mvx.RegisterSingleton<IMvxFormsViewPresenter>(presenter);
var presenter2 = Mvx.GetSingleton<IMvxViewPresenter>() as MvxFormsUwpViewPresenter;
return presenter;
}
When updating to MvvmCross 5.3.2 for UWP, the presenter needs to resolve as IMvxFormsViewPresenter rather than IMvxViewPresenter. Change the interface type and it should load properly.
public MainPage()
{
this.InitializeComponent();
var start = Mvx.Resolve<IMvxAppStart>();
start.Start();
var presenter = Mvx.Resolve<IMvxFormsViewPresenter>() as MvxFormsUwpViewPresenter;
LoadApplication(presenter.FormsApplication);
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}

How to tell parent to pop current modal and redirect to NavigationPage

I have a Xamarin application, where my MainPage is a navigationpage (contentpage). In this navigationpage I open modals.
In the modals I open in my MainPage, I want to use Navigation.PushAsync but this requires the NavigationPage context which a modal page is not.
I've read that the flow would be:
Tell parent/caller to pop current visible modal
Tell parent/caller
to navigate to the page I want, using Navigation.PushAsync
However, how do you do this? How do you tell the parent to do this and actually do the navigation?
I would use event handlers to get this job done:
Here is an example code:
App.cs
public class App : Application
{
public App()
{
MainPage = new NavigationPage(new PushedNavigationPage());
}
}
PushedNavigationPage Page:
public class PushedNavigationPage: ContentPage
{
public PusehdNavigationPage()
{
}
public void DoPushModalOnButtonClick()
{
var pushedModalPage = new PushedModalPage();
pushedModalPage.DoPush += HandleDoPush;
Navigation.PushModalAsync(pushedModalPage);
}
private void HandleDoPush(object sender, EventArgs e)
{
Navigation.PopModalAsync();
Navigation.PushAsync(//the page you want to push)
}
}
PushedModalPage
public class PushedModalPage : ContentPage
{
public event EventHandler DoPush;
//call this on putton click or whenever you want
private void OnDoPush()
{
if (DoPush!= null)
{
DoPush(this, EventArgs.Empty);
}
}
}
I have not awaiting the async methods but it is better to do so and this is just an example, but I think you should get the idea..

Resources