到目前为止,为了更改 TitleBarTextColor,我尝试了很多,我的代码现在确实更改了后退按钮的颜色和屏幕顶部的区域,但是标题!
在我的 AppDelegate FinishedLaunching 函数中(在 Forms.Init() 之后和 LoadApplication() 之前):
UINavigationBar.Appearance.SetTitleTextAttributes(new UITextAttributes
{
TextColor = UIColor.White
});
在我的视图模型中:
(App.Current.MainPage as NavigationPage).BarBackgroundColor = Color.FromHex("#990000");
(App.Current.MainPage as NavigationPage).BarTextColor = Color.White;
这就是我在页面(不是所有页面)之间导航的方式:
await _navigationService.NavigateAsync(new Uri("http://wwww.x.com/NavigationPage/TabbedNavigationPage?selectedTab=XPage/Document", UriKind.Absolute));
我什至尝试在 Prism 导航之后调用 ViewModel 代码,但它不起作用......我是一个初学者,并没有完全理解 Prism 和/或 Xamarin Forms。
[编辑] -> 我还尝试创建一个新类并从 继承它NavigationPage
,BarTextColor
在其构造函数中设置并在导航中使用该类,如下所示:await _navigationService.NavigateAsync(new Uri("http://wwww.x.com/NEWCLASSCREATED/TabbedNavigationPage?selectedTab=XPage/Document", UriKind.Absolute));
但是,如您所知,它仍然无法正常工作。
这是一张图片;)
谢谢您的支持!
所以我最终设法解决了这个问题......
我必须做的是创建一个自定义,Content Page
因为所有其他解决方案都不起作用。所以我只在我的 iOS 项目中创建了这个自定义渲染器:
[assembly: ExportRenderer(typeof(ContentPage), typeof(CustomContentPageRenderer))]
namespace TestProject.iOS.Bll.Utils.Renderers
{
public class CustomContentPageRenderer : PageRenderer
{
public override void DidMoveToParentViewController(UIViewController parent)
{
base.WillMoveToParentViewController(parent);
var titleView = new UITextView();
var page = this.Element as ContentPage;
try
{
if (!string.IsNullOrEmpty(page.Title))
{
titleView.Text = page.Title;
titleView.TextColor = UIColor.White;
titleView.Font = UIFont.SystemFontOfSize(17, UIFontWeight.Regular);
var bgColor = page.BackgroundColor;
titleView.BackgroundColor = UIColor.FromRGBA((int)bgColor.R, (int)bgColor.G, (int)bgColor.B, 0);
parent.NavigationItem.TitleView = titleView;
parent.NavigationItem.TitleView.ContentMode = UIViewContentMode.ScaleAspectFit;
}
}
catch (Exception e)
{
}
}
}
}
我还删除了之前放在我的AppDelegate
文件和文件中的所有代码App.xaml.cs
。我留下了代码,ViewModels
因为它将后退按钮更改为白色,并且我删除了NagivationPage
之前创建的新类。
我将解释为什么我做了你在那里看到的一些事情:
要更改标题,我创建了一个UITextView()
并将其设置为我NavigationItem.TitleView
的父页面。我设置titleView.Text = page.Title;
是因为我的原始页面已经有一个标题,所以我只是重复使用它。而且backgroundcolor
我必须做所有这些,所以该backgroundcolor
属性按我想要的方式工作。
而这个DidMoveToParentViewController
功能正是为了它可以在NavigationAsync
Prism之前完成所有这些。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句