gpt4 book ai didi

c# - 带有 MasterDetails 页面和导航的 Xamarin.forms

转载 作者:行者123 更新时间:2023-11-30 14:50:39 25 4
gpt4 key购买 nike

我有一个应用程序,我希望在其中有一个包含 2 个选项的母版页和每个详细信息页面的 toolbarItems。

enter image description here

例如,这里我有一个设置页面作为我的详细信息页面,它有两个 tollbaritems 保存和取消。这是用户必须输入必须保存的用户数据的形式。因此,在单击保存 toolbarItem 时,我想保存数据并将用户重定向到母版页列表选项(设置、登录)中未包含的其他页面(比如“PageA”)

我该怎么做?如果我执行 await Navigation.PushAsync(new PageA());,我不会获得主页。我返回图标以返回到主详细信息页面。但我想将用户重定向到一个页面,该页面也应该有 masterdetails 选项(设置和登录)。

我也在点击保存按钮时尝试过这个:

MasterDetailPage MDPage = new MasterDetailPage();
MDPage.Detail = new NavigationPage(new PageA());
MDPage.IsPresented = false;

提前致谢。

编辑这是我的代码:

母版.cs

public ListView ListView { get { return listView; } }

ListView listView;
public MasterPage()
{
var masterPageItems = new List<MasterPageItem>();
masterPageItems.Add(new MasterPageItem
{
Title = "Settings",
//IconSource = "Settings.png",
TargetType = typeof(TestPage)
});

listView = new ListView
{
ItemsSource = masterPageItems,
ItemTemplate = new DataTemplate(() =>
{
var imageCell = new ImageCell();
imageCell.SetBinding(TextCell.TextProperty, "Title");
imageCell.SetBinding(ImageCell.ImageSourceProperty, "IconSource");
return imageCell;
}),
VerticalOptions = LayoutOptions.FillAndExpand,
SeparatorVisibility = SeparatorVisibility.None
};

Padding = new Thickness(0, 40, 0, 0);
// Icon = "hamburger.png";
Title = "Menu";
BackgroundColor = Color.FromHex("#4F6276");

Content = new StackLayout
{
VerticalOptions = LayoutOptions.FillAndExpand,
Children = {
listView
}
};
}

MasterDetailsPage:

 public class MainPage : MasterDetailPage
{
public MainPage()
{
MasterPage masterPage;
masterPage = new MasterPage();
Master = masterPage;
Detail = new NavigationPage(new TestPage())
{
//Tint = Color.Red // put your color here
BarBackgroundColor = Color.FromRgb(172, 183, 193),//Color.FromHex("#9E9E9E"),
BarTextColor = Color.Black,
BackgroundColor = Color.White
};
}
}

测试页:

public class TestPage : ContentPage
{
public TestPage()
{
Button test = new Button();
test.Clicked += test_Clicked;
test.Text = "Save";
Button test1 = new Button();
test1.Clicked += test1_Clicked;
test1.Text = "cancel";
Content = new StackLayout
{
Children = {
test,
test1
}
};
}

void test_Clicked(object sender, EventArgs e)
{
//redirect to new page(Page1)
}
void test1_Clicked(object sender, EventArgs e)
{
//redirect to new page(Page2)
}
}

我想从 onclick 事件重定向到一个新页面,比如 page1。我如何实现这一目标?

最佳答案

保存设置后,您应该更改现有 MasterDetailPage 实例的详细信息。

我认为最简单明了的方法是使用 MessagingCenter :

在设置页面:

MessagingCenter.Send(new OpenMyPageMessage(), OpenMyPageMessage.Key);

您的主从页面:

protected override void OnAppearing()
{
MessagingCenter.Subscribe<OpenMyPageMessage>(this, OpenMyPageMessage.Key, (sender) =>
{
Detail = new YourAnotherPage();
});
}

protected override void OnDisappearing()
{
MessagingCenter.Unsubscribe<OpenMyPageMessage>(this, OpenMyPageMessage.Key);
}

OpenMyPageMes​​sage 只是一个简单的类:

public class OpenMyPageMessage
{
public static string Key = "OpenMyPageMessage";
}

结果: Preview

关于c# - 带有 MasterDetails 页面和导航的 Xamarin.forms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36172577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com