【问题标题】:Is there a way in Xamarin to avoid command repetition?Xamarin 有没有办法避免命令重复?
【发布时间】:2020-12-02 12:55:25
【问题描述】:

我有 10 个 Xamarin 页面/视图,其中有一个命令/代码块在所有这些页面的代码隐藏中重复。它在所有页面上都做同样的事情,只是弹出堆栈直到到达根目录。

我尝试扩展基类 (ContentPage) 以创建自定义类,但由于它是部分类而出错?

错误信息:

"Partial declarations of 'TestPage' must not specify different base classes"

有没有办法避免在所有 10 个类中重写相同的方法?另外,我正在使用带有 Prism 的 Xamarin

【问题讨论】:

  • 如果您在后面的代码中更改页面的基类,您还需要更新 XAML 中的基类

标签: xamarin xamarin.forms prism


【解决方案1】:

如果你想用 xaml 做一个基本页面,你可以使用一个基本页面。

BasePage.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="App2.BasePage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
BackgroundColor="AliceBlue"
mc:Ignorable="d" />

MainPage.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<app2:BasePage
x:Class="App2.MainPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:app2="clr-namespace:App2"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<app2:BasePage.Content>
    <StackLayout>
        <Label Text="Wellcome to MainPage!!!" />
    </StackLayout>
</app2:BasePage.Content>

</app2:BasePage>

MainPage.xaml.cs:

    public partial class MainPage : BasePage
{
    public MainPage()
    {
        InitializeComponent();
    }
}

【讨论】:

    猜你喜欢
    • 2021-12-15
    • 2019-09-07
    • 1970-01-01
    • 2022-11-16
    • 2019-05-29
    • 2011-09-13
    • 2012-06-20
    • 2014-11-17
    • 2020-11-20
    相关资源
    最近更新 更多