.NET MAUI调用Navigation.PushAsync仅改标题不跳转页面问题
.NET 8 MAUI安卓模拟器页面跳转异常问题
我有一个.NET 8 MAUI项目,采用底部标签页布局,每个标签页均嵌套NavigationPage。在ImageList页面中,我设置了两个工具栏按钮,分别用于打开设备相机或相册。当执行代码跳转至PicEdit页面时,仅页面标题发生变化,页面内容无法显示,该问题出现在Win11系统的安卓模拟器中。我已尝试多种方法并搜索资料,但均未解决问题,即使将PicEdit页面简化为仅含一个Label也无效。
感谢您的帮助(TIA)
标签页代码
public class TabbedNavPage : Microsoft.Maui.Controls.TabbedPage { public TabbedNavPage() { On<Microsoft.Maui.Controls.PlatformConfiguration.Android>().SetToolbarPlacement(ToolbarPlacement.Bottom); var ratePage = new NavigationPage(new MainPage()) { Title = "Rate", IconImageSource = "rate.png" }; var login = new NavigationPage(new Login()) { Title = "Login & Account", IconImageSource = "account.png" }; var imageList = new NavigationPage(new ImageList()) { Title = "Image List", IconImageSource = "list.png" }; var img = new NavigationPage(new PicEdit()); Children.Add(ratePage); Children.Add(login); Children.Add(imageList); Children.Add(img); } }
工具栏按钮代码
<ContentPage.ToolbarItems> <ToolbarItem Text="Add from camera" x:Name="CameraAdd" IconImageSource="camera.png" Clicked="CameraAdd_Clicked"/> <ToolbarItem Text="Add from photo library" x:Name="PhotoLibraryAdd" IconImageSource="imagelist.png" Clicked="PhotoLibraryAdd_Clicked"/> </ContentPage.ToolbarItems>
打开相机的代码
private async void CameraAdd_Clicked(object sender, EventArgs e) { await GetImageEditorCameraSourceAsync(); } private async Task GetImageEditorCameraSourceAsync() { var status = await Permissions.CheckStatusAsync<Permissions.Camera>(); if (status != PermissionStatus.Granted) { // 如果相机权限未授予,请求权限 status = await Permissions.RequestAsync<Permissions.Camera>(); if (status != PermissionStatus.Granted) { // 如果用户拒绝权限,可显示提示或执行其他操作 return; } } var photo = await MediaPicker.Default.CapturePhotoAsync(); if (photo != null) { var img = ImageSource.FromStream(() => photo.OpenReadAsync().Result); await Navigation.PushAsync(new PicEdit() { ImageSource = img }); } }
PicEdit页面代码
<ContentPage.Content> <VerticalStackLayout> <Label Text="Hi" /> </VerticalStackLayout> </ContentPage.Content>
补充内容(11月30日美国东部时间上午9:30)
以下是仅点击按钮跳转页面的事件处理器,可正常工作:
private async void Camera_Clicked(object sender, EventArgs e) { await Navigation.PushAsync(new PicEdit()); }
但使用以下代码调用相机并跳转时,则无法正常工作。我推测是打开相机/相册后,MAUI内部的栈或全局变量状态异常导致的:
private async void Camera_Clicked(object sender, EventArgs e) { if (MediaPicker.Default.IsCaptureSupported) { FileResult photo = await MediaPicker.Default.CapturePhotoAsync(); if (photo != null) { // 将文件保存到本地存储 string localFilePath = Path.Combine(FileSystem.CacheDirectory, photo.FileName); using Stream sourceStream = await photo.OpenReadAsync(); using FileStream localFileStream = File.OpenWrite(localFilePath); await sourceStream.CopyToAsync(localFileStream); await Navigation.PushAsync(new PicEdit()); } } }
内容的提问来源于stack exchange,提问作者Wallace B. McClure
相关产品推荐
相关产品推荐

