【问题标题】:Xamarin.Forms Different View Per Platform and DeviceXamarin.Forms 每个平台和设备的不同视图
【发布时间】:2020-04-30 03:41:44
【问题描述】:

我在 Xamarin.Forms 中有一个项目,我正在尝试创建 4 个视图:Android 手机、Android 平板电脑、iPhone 和 iPad。我尝试了一个小例子,在 XAML 中是这样的:

        <ContentPage.Content>
            <ContentView>
                <OnPlatform x:TypeArguments="View">
                    <On Platform="iOS">
                        <!-- Use view for iOS here -->
                        <OnIdiom x:TypeArguments="View">
                            <OnIdiom.Phone>
                                <Frame x:Name="MainFrameiPhone">
                                    <StackLayout BackgroundColor="#FF00FF">       
                                    </StackLayout>
                                </Frame>
                            </OnIdiom.Phone>
                            <OnIdiom.Tablet>
                                <Frame x:Name="MainFrameiPad">
                                    <StackLayout BackgroundColor="#00FF00">       
                                    </StackLayout>
                                </Frame>
                            </OnIdiom.Tablet>                        
                        </OnIdiom>                     
                    </On>
                    <!--Same for android -->
                    <On Platform="Android">
                        ...
                    </On>
                </OnPlatform>
            </ContentView>           
        </ContentPage.Content>  

但我不想重命名我在 4 个视图中使用的每个框架和组件(例如 MainFrameiPhoneMainFrameiPad)。

我做得对吗?或者我可以创建 4 个视图并从后面的代码中调用每个视图吗?

有什么更好的方法来做到这一点?

谢谢!

【问题讨论】:

    标签: xamarin.forms


    【解决方案1】:

    你可以使用ControlTemplate

    public class MainFrameiPhone: Frame
    {
      public TealTemplate ()
      {
        ...
        // set the content here 
      }
    }
    
    public class MainFrameiPad: Frame
    {
      ...
    }
    

    在内容页面中

    ControlTemplate mainFrameiPhone= new ControlTemplate(typeof(MainFrameiPhone));
    ControlTemplate mainFrameiPad= new ControlTemplate(typeof(MainFrameiPad));
    //...
    
    public MainPage()
    {
       //...
            ContentView content = new ContentView() {
    
                Content = {
    
                    //...
                }
    
    
    
            };
    
            if(Device.RuntimePlatform=="iOS")
            {
                if(Device.Idiom==TargetIdiom.Phone)
                {
                    content.ControlTemplate = mainFrameiPhone;
                }
                else if(Device.Idiom == TargetIdiom.Phone)
                {
                    content.ControlTemplate = mainFrameiPad;
                }
            }
    
            else if(Device.RuntimePlatform == "Android")
            {
                if (Device.Idiom == TargetIdiom.Phone)
                {
                    content.ControlTemplate = xxx;
                }
                else if (Device.Idiom == TargetIdiom.Phone)
                {
                    content.ControlTemplate = xxx;
                }
            }
    
            else
            {
                content.ControlTemplate = xxx;
            }
    
            Content = content;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-18
      • 2021-05-27
      • 2011-12-23
      • 2022-11-17
      相关资源
      最近更新 更多