代码之家  ›  专栏  ›  技术社区  ›  KalleP

使用Xamarin格式的Prism在捕获的参数上添加硬编码文本

  •  0
  • KalleP  · 技术社区  · 6 年前

    这是我如何从第二个ViewModel捕获模型:

    public override void OnNavigatedTo(INavigationParameters parameters)
        {
            TodoItem = (TodoItem)parameters["Todo"];
        }
    

    Title + "Test" = value.name 但它给出空值:

    private TodoItem _todoItem;
    public TodoItem TodoItem
        {
            get => _todoItem;
            set
            {
                _todoItem = value;
                Title = value.name;
            }
        }
    

    这是我从Xaml绑定的标题字符串:

    private string _title;
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   KalleP    6 年前

    将此设置为@Junior Jiang-MSFT提到的有效:

    var title = value.name;
                Title = title + "Test";
    
        2
  •  0
  •   Jesus Angulo    6 年前

    private TodoItem _todoItem;
    public TodoItem TodoItem
    {
       get => _todoItem;
       set
       {
         _todoItem = value;
         Title = value?.name ?? "";
       }
     }
    

    如果您想将方法更改为更具声明性的方法,可以使用转换器。

        public class PrefixConverter : IValueConverter
        {
            public string Prefix { get; set; }
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return Prefix + value?.ToString();
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    

    所以,您可以在XAML中使用

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:conv="clr-namespace:Sample.Converters"
                 x:Class="Sample.MainPage"
                 xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
                 prism:ViewModelLocator.AutowireViewModel="True"
                 Title="{Binding TodoItem.name,Converter={StaticResource PrefixConverter}}">
        <ContentPage.Resources>
            <ResourceDictionary>
                <conv:PrefixConverter x:Key="PrefixConverter" Prefix="Test"/>
            </ResourceDictionary>
        </ContentPage.Resources>