Xamarin中是否有一种方法可以避免命令重复?

时间:2020-08-12 17:55:30

标签: xamarin xamarin.forms prism

我有10个Xamarin页面/视图,有一个命令/代码块在所有这些代码的代码后面都重复了。而且它在所有页面上都执行相同的操作,只是弹出堆栈,直到到达根目录为止。

我曾尝试将基类(ContentPage)扩展为自定义类,但由于它是局部类而出现错误?

错误消息:

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

有没有一种方法可以避免在所有10个类中重写相同的Method?另外,我将Xamarin与Prism一起使用

1 个答案:

答案 0 :(得分:0)

如果要使用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();
    }
}