繁体   English   中英

我有一个关于 Xamarin.Forms 导航的问题。 如何将变量值从一页传递到另一页?

[英]I have a question regarding Xamarin.Forms Navigation. How to pass variable values from one page to other?

我有一个带有以下集合视图的 MainPage.xaml 文件

<CollectionView ItemsSource="{Binding AllNotes}" 
                            SelectionMode="Single"
                            SelectedItem="{Binding SelectedNote}"
                            SelectionChangedCommand="{Binding SelectedNoteChangedCommand}"
                            Grid.Row="2" Grid.ColumnSpan="2">
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <StackLayout>
                            <Frame>
                                <Label Text="{Binding .}" FontSize="Title"/>
                            </Frame>
                        </StackLayout>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>

在 MainPageView.cs 文件中,我使用以下代码获取了所选笔记的值

public string selectedNote;
public string SelectedNote
{
    get => selectedNote;
    set
    {
        selectedNote = value;

        var args = new PropertyChangedEventArgs(nameof(SelectedNote));

        PropertyChanged?.Invoke(this, args);
    }
}

SelectedNoteChangeCommand重定向到DetailPage应打印所选笔记的位置。 SelectedNoteChangeCommand具有以下代码

SelectedNoteChangedCommand = new Command(async () =>
    {
        var detailVM = new ListPageViewDetail.DetailPageView(SelectedNote);
        var detailPage = new List.DetailPage();

        detailPage.BindingContext = detailVM;
        await Application.Current.MainPage.Navigation.PushAsync(detailPage);
    });

现在,如果我在同一页面上显示SelectedNote的值,它会显示,但不会显示在DetailPage的 label 字段中

DetailPage.xaml 有一个 label 字段为

<Label Text="{Binding NoteText}" FontSize="Title" Grid.Row="0"
                   VerticalOptions="CenterAndExpand"
                   HorizontalOptions="CenterAndExpand" />

DetailPageView.cs 文件的构造函数为

public DetailPageView(string note)
{
    NoteText = note;
    DismissPageCommand = new Command(async () =>
        {
            await Application.Current.MainPage.Navigation.PopAsync();
        });
}

现在我想问的是如何将SelectedNote变量值传递给其他页面? NoteText 或注释具有空值。

根据您的描述,您想在 ContentPage 之间传递值,我创建了您可以查看的示例。

第一个内容页:

 <CollectionView
            ItemsLayout="VerticalList"
            ItemsSource="{Binding AllNotes}"
            SelectedItem="{Binding selectednote}"
            SelectionChangedCommand="{Binding SelectedNoteChangedCommand}"
            SelectionMode="Single">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <StackLayout>
                        <Label Text="{Binding .}" />
                    </StackLayout>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>

 public partial class Page4 : ContentPage, INotifyPropertyChanged
{
    public  ObservableCollection<string> AllNotes { get; set; }
    private string _selectednote;
    public string selectednote
    {
        get { return _selectednote; }
        set
        {
            _selectednote = value;
            RaisePropertyChanged("selectednote");
        }
    }

    public RelayCommand1 SelectedNoteChangedCommand { get; set; }
    public Page4()
    {
        InitializeComponent();
        AllNotes = new ObservableCollection<string>()
        {
            "test 1",
            "test 2",
           "test 3",
            "test 4",
            "test 5",
            "test 6"

        };
        selectednote = AllNotes[0];
        SelectedNoteChangedCommand = new RelayCommand1(obj=>passdata((string)selectednote));
        this.BindingContext = this;
    }

    private void passdata(string selectednote)
    {
        Navigation.PushAsync(new Page5(selectednote));
    }
    public event PropertyChangedEventHandler PropertyChanged;


    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

RelayCommand1 class,继承ICommand,可以传参数。

public class RelayCommand1 : ICommand
{
    private readonly Predicate<object> _canExecute;
    private readonly Action<object> _execute;

    public RelayCommand1(Action<object> execute)
        : this(execute, null)
    {
    }

    public RelayCommand1(Action<object> execute, Predicate<object> canExecute)
    {
        _execute = execute;
        _canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }

    public event EventHandler CanExecuteChanged;


    public void Execute(object parameter)
    {
        _execute(parameter);
    }
}

第二个内容页:

 <StackLayout>
        <Label
            HorizontalOptions="CenterAndExpand"
            Text="{Binding .}"
            VerticalOptions="CenterAndExpand" />
    </StackLayout>

  public Page5(string str)
    {
        InitializeComponent();
        this.BindingContext = str;
    }

截图:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM