微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 androidx.navigation 时由 Activity 重置的工具栏标题

如何解决使用 androidx.navigation 时由 Activity 重置的工具栏标题

使用 androidx.navigation 在 namespace Permisson { /// <summary> /// An empty page that can be used on its own or navigated to within a Frame. /// </summary> public sealed partial class MainPage : Page { public viewmodel viewmodel { get; set; } public MainPage() { this.InitializeComponent(); this.viewmodel = new viewmodel(); } private void Button_Click(object sender,RoutedEventArgs e) { var v = viewmodel.DataRef[0]; v.Val = 9; } private void Button_Click_1(object sender,RoutedEventArgs e) { var v = viewmodel.DataRef[1]; v.Val = 10; } } public class viewmodel { private ObservableCollection<ValWrapper> dataRef = new ObservableCollection<ValWrapper>() { new ValWrapper {Val=22,Brush=new SolidColorBrush (Colors.Green),IsUpdate = false },new ValWrapper {Val=25,IsUpdate = false},new ValWrapper {Val=35,new ValWrapper {Val=45,new ValWrapper {Val=55,new ValWrapper {Val=65,IsUpdate = false } }; public ObservableCollection<ValWrapper> DataRef { get { return dataRef; } } } public class ColorConverter : IValueConverter { public object Convert(object value,Type targettype,object parameter,string language) { var color = new SolidColorBrush(); if ((bool)value) { color.Color = Colors.Red; } else { color.Color = Colors.Green; } return color; } public object ConvertBack(object value,string language) { throw new NotImplementedException(); } } public class ValWrapper : INotifyPropertyChanged { private int val; private SolidColorBrush brush; public SolidColorBrush Brush { get { return brush; } set { brush = value; RaisePropertyChanged(); } } private int _tempValue; public int Val { get { return val; } set { if(_tempValue != value && _tempValue != 0) { IsUpdate = true; } val = value; RaisePropertyChanged(); _tempValue = val; } } private bool _isUpdate; public bool IsUpdate { set { _isUpdate = value; RaisePropertyChanged(); } get { return _isUpdate; } } public event PropertyChangedEventHandler PropertyChanged; public void RaisePropertyChanged([CallerMemberName] string propertyname = null) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this,new PropertyChangedEventArgs(propertyname)); } } } } 中使用 android.label 时会自动设置工具栏标题。这工作正常,直到发生配置更改(例如旋转设备)。在此之后,工具栏标题将重置为应用标题

在旋转后导航的 nav_graph 设置标题后,Activity 似乎正在重新设置 Activity.onPostCreate() 中的标题

解决方法

您必须设置带有导航图和支持操作栏的工具栏。

val appCompat = requireActivity() as AppCompatActivity
appCompat.setSupportActionBar(toolbar)
appCompat.setupActionBarWithNavController(navController)

查看 this 了解更多信息。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。