XAML Xamarin.表单动态更改母版页内容

siv3szwd  于 2023-11-14  发布在  其他
关注(0)|答案(2)|浏览(154)

我有一个简单的应用程序与主细节页面。
它看起来很像:x1c 0d1x
我想动态地改变主页面的内容。例如,在细节页面上有一些按钮,然后你点击它-从主页面消失的一些项目。
我试图找到一些项目属性,如item1.isVisible=False,但一无所获。
有什么办法可以让我做吗?
MainPage代码:

public partial class MainPage : MasterDetailPage
{
    public MainPage()
    {
        InitializeComponent();
        masterPage.listView.ItemSelected += OnItemSelected;
    }

    void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        var item = e.SelectedItem as MasterPageItem;
        if (item != null)
        {
            Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));
            masterPage.listView.SelectedItem = null;
            IsPresented = false;
        }
    }
}

字符串
MainPage XAML:

<MasterDetailPage xmlns=....>
<MasterDetailPage.Master>
    <views:MasterPage x:Name="masterPage" />
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
    <NavigationPage>
        <x:Arguments>
            <views:SomePage/>
        </x:Arguments>
    </NavigationPage>
</MasterDetailPage.Detail>


MasterPage XAML:

<StackLayout>
    <ListView x:Name="listView" x:FieldModifier="public" Margin="10,50,0,0">
        <ListView.ItemsSource>
            <x:Array Type="{x:Type local:MasterPageItem}">
                <local:MasterPageItem Title="Home" TargetType="{x:Type views:HomePage}" IconSource="nav_icon_home"/>
                <local:MasterPageItem Title="Settings" TargetType="{x:Type views:HomePage}" IconSource="nav_icon_settings" />
                <local:MasterPageItem Title="My Profile" TargetType="{x:Type views:HomePage}" IconSource="nav_icon_profile" />
                <local:MasterPageItem Title="Help" TargetType="{x:Type views:HomePage}" IconSource="nav_icon_help" />
                <local:MasterPageItem Title="About" TargetType="{x:Type views:AboutPage}" IconSource="nav_icon_about"/>
            </x:Array>
        </ListView.ItemsSource>
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Grid Padding="10,10,10,10" ColumnSpacing="10">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="30"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Image Source="{Binding IconSource}"/>
                        <Label Grid.Column="1" Text="{Binding Title}" TextColor="White" VerticalOptions="Center"/>
                    </Grid>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</StackLayout>


MasterPage项目:

public class MasterPageItem
{
    public string Title { get; set; }

    public string IconSource { get; set; }

    public Type TargetType { get; set; }
}


所以我在MasterPageItem中实现了INotifyPropertyChanged

public class MasterPageItem : INotifyPropertyChanged
{
    public string Title { get; set; }

    public string IconSource { get; set; }

    public Type TargetType { get; set; }

    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    private bool isVisible;

    public bool IsVisible
    {
        get { return isVisible; }
        set
        {
            isVisible = value;
            PropertyChanged(this, new PropertyChangedEventArgs("IsVisible"));
        }
    }

}


但是我如何将它绑定到我的MasterPage

yrdbyhpb

yrdbyhpb1#

最后,我实现了两个不同的MasterPages,所以我可以在需要的时候在它们之间切换。
所以我有2个MainPages,第一个是指MasterPage1

<MasterDetailPage.Master>
    <views:MasterPage1 x:Name="masterPage1" />
</MasterDetailPage.Master>

<MasterDetailPage.Detail>
<NavigationPage>
    <x:Arguments>
        <views:SomePage1/>
    </x:Arguments>
</NavigationPage>
</MasterDetailPage.Detail>

字符串
第二个MainPage2-到MasterPage2

<MasterDetailPage.Master>
    <views:MasterPage2 x:Name="masterPage2" />
</MasterDetailPage.Master>

<MasterDetailPage.Detail>
<NavigationPage>
    <x:Arguments>
        <views:SomePage2/>
    </x:Arguments>
</NavigationPage>
</MasterDetailPage.Detail>


我知道这不是最好的解决办法,但对我来说已经足够了。

mwg9r5ms

mwg9r5ms2#

一般来说,您可以通过更改Xamarin.Forms.MasterDetailPageMaster-属性来更改母版页的视图。如果您将Xamarin.Forms.ContentPage分配给Master-属性,则可以通过((ContentPage)YourMasterDetailPage.Master).Content访问它。
我相信这个问题已经回答了100次,并遵循xamarin表单的基本结构。
从这里从头开始学习MasterDetailPage:https://learn.microsoft.com/en-US/xamarin/xamarin-forms/app-fundamentals/navigation/master-detail-page
既然你现在提供了一些代码,这里有一个小例子:

void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        var item = e.SelectedItem as MasterPageItem;
        if (item != null)
        {
            Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));
            item.IsVisible = false; //this would make the clicked item invisible
            masterPage.listView.SelectedItem = null;
            IsPresented = false;
        }
    }

字符串

相关问题