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

Xamarin:如何将信息从提示传递到视图模型

如何解决Xamarin:如何将信息从提示传递到视图模型

我是Xamarin的初学者,我正在尝试创建一个页面,您可以在其中存储财务状况条目

  1. 单击工具栏按钮
  2. 出现提示
  3. 它已添加到可观察的集合中。 但我似乎无法找出一种方法,将信息从提示传递到viewmodel集合。

这是代码

xaml:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:Jupiter.Pages.Function_Pages.FinancePage"
             x:Class="Jupiter.Pages.Function_Pages.Finance"
             BackgroundColor="Mediumpurple">
  
    <ContentPage.toolbaritems>
        <ToolbarItem Text="Add Entry" Clicked="{Binding AddFentryCommand}"></ToolbarItem>
    </ContentPage.toolbaritems>
    <ContentPage.Content>
        <StackLayout >
            <ListView ItemsSource="{Binding FinanceEntries}" BackgroundColor="Mediumpurple">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextCell Text="{Binding Name}" Detail="{Binding Amount}"></TextCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
            
        </StackLayout>
        
    </ContentPage.Content>
</ContentPage>

#C:

public partial class Finance : ContentPage
{
    ///Body

    public ListView LV;

    public Finance()
    {
        BindingContext = new Financeviewmodel();
        InitializeComponent();
      
    }
    private async void goHome(object sender,EventArgs e)
    {
        await Navigation.PushModalAsync(new MenuPage());
    }
    public async void AddEntry(object sender,EventArgs e)
    {
        string result = await displayPromptAsync("Question 1","What's the name of the entry?");
        string result1 = await displayPromptAsync("Question 2","What's the amount?","Ok","Cancel",null,10,Keyboard.Numeric,"");
        int x = int.Parse(result1)

    }
    ///View Model
    class Financeviewmodel : INotifyPropertyChanged
    {

        string _filename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),"finentries.txt");
        public int currententry = 0;
        public ObservableCollection<FinanceEntry> FinanceEntries { get; set; }
        public ICommand AddFentryCommand { get; set; }
        public Financeviewmodel()
        {
            AddFentryCommand = new Command(AddFentry);
        }
        public async void AddFentry()
        {
            currententry++;
            FinanceEntries.Add(new FinanceEntry { Name= $"entry {result}",Amount=x });
        }

#region INotifyPropertyChanged Implementation
public event PropertyChangedEventHandler PropertyChanged;
        void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            if (PropertyChanged == null)
                return;

            PropertyChanged.Invoke(this,new PropertyChangedEventArgs(propertyName));
        }
        #endregion
    }

}

解决方法

首先,保留对您的VM的引用

public ListView LV;

private FinanceViewModel VM;

public Finance()
{
    BindingContext = VM = new FinanceViewModel();
    InitializeComponent();
  
}

那么你可以做到

public async void AddEntry(object sender,EventArgs e)
{
    string result = await DisplayPromptAsync("Question 1","What's the name of the entry?");
    string result1 = await DisplayPromptAsync("Question 2","What's the amount?");

    VM.FinanceEntries.Add(new FEntry(result,result1));
}

您需要初始化FinanceEntries,否则它将为null,并且您的应用将崩溃

    public ObservableCollection<FinanceEntry> FinanceEntries { get; set; }
    public ICommand AddFentryCommand { get; set; }

    public FinanceViewModel()
    {
        AddFentryCommand = new Command(AddFentry);
        FinanceEntries = new ObservableCollection<FinanceEntry>();
    }

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