xamarin forms - Open the Form ByName sample app

/////******* mainpage.xaml.cs  
 async void OnButtonClicked(object sender, EventArgs e)
        {
            try
            {
            Button btn = (Button)sender;
            string id = btn.StyleId;
            Assembly assembly = GetType().GetTypeInfo().Assembly;
            Type pageType = assembly.GetType("AppOpentheFormByName." + id);
            Page page = (Page)Activator.CreateInstance(pageType);
            await Navigation.PushAsync(page);
            }
            catch (Exception ex)
            {
                var s = ex.Message;
            }
        }

///////////*********MainPage.xaml
 <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="AppOpentheFormByName.MainPage">

    <StackLayout>
        <Frame BackgroundColor="#2196F3" Padding="24" CornerRadius="0">
            <Label Text="Welcome to Xamarin.Forms!" HorizontalTextAlignment="Center" TextColor="White" FontSize="36"/>
        </Frame>
        <Label Text="Start developing now" FontSize="Title" Padding="30,10,30,10"/>


        <Button Text="Open Page1"
                StyleId="Page1"
                Font="Large"
                BorderWidth="1"
                HorizontalOptions="Center"
                VerticalOptions="CenterAndExpand"
                Clicked="OnButtonClicked" />
        
        
    </StackLayout>

</ContentPage>


///////////********* app.xaml.cs
  public App()
        {
            InitializeComponent();

            //MainPage = new MainPage();
            MainPage = new NavigationPage(new MainPage());//because navigate other pages from mainpage.

        }

//////*********** GitHub public :
https://github.com/doktoralban/AppOpentheFormByName

Yorumlar