Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 除非重新加载UI HotReload,否则Xamarin.Forms SwipeView CommandParameter为null_C#_Xamarin_Xamarin.forms - Fatal编程技术网

C# 除非重新加载UI HotReload,否则Xamarin.Forms SwipeView CommandParameter为null

C# 除非重新加载UI HotReload,否则Xamarin.Forms SwipeView CommandParameter为null,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,好的,我有一个CollectionView绑定到一个“Show”对象的集合。这个集合视图有相当多的内容。我有一个命令,用于当用户选择一个节目(单点击重定向到详细信息页面)、选择多个项目以执行多项目删除,以及滑动以对每个单独的节目执行基本发布功能。除了刷卡以外,一切都正常。出于某种原因,当启动其中一个滑动命令时,传递给该命令的“Show”对象为null。但是,我已经为此启用了热重新加载,如果我所做的只是保存xaml文件,它就可以正常工作。传递的显示不是空的,它可以像我期望的那样工作。当我保存xam

好的,我有一个CollectionView绑定到一个“Show”对象的集合。这个集合视图有相当多的内容。我有一个命令,用于当用户选择一个节目(单点击重定向到详细信息页面)、选择多个项目以执行多项目删除,以及滑动以对每个单独的节目执行基本发布功能。除了刷卡以外,一切都正常。出于某种原因,当启动其中一个滑动命令时,传递给该命令的“Show”对象为null。但是,我已经为此启用了热重新加载,如果我所做的只是保存xaml文件,它就可以正常工作。传递的显示不是空的,它可以像我期望的那样工作。当我保存xaml文件时,我甚至不需要更改任何内容,我只是简单地保存,没有新的更改(ctrl+s),代码开始工作,我的滑动项命令也很好。以下是我的代码参考:

xaml:

视图模型:

public class ShowsViewModel : BaseViewModel
    {
        public ObservableCollection<Show> Shows { get; }
        public ObservableCollection<object> SelectedShows { get; }

        public Command SelectionChangedCommand { get; }
        public Command ShowSelectedCommand { get; }
        public Command DeleteMultipleShowsCommand { get; }
        public Command AddShowCommand { get; }
        public Command LoadShowsCommand { get; }
        public Command SwipeIsPublishCommand { get; }
        public Command SwipeIsActiveCommand { get; }
        public Command SwipeIsSoldOutCommand { get; }

        public ShowsViewModel()
        {
            Title = "Shows";
            Shows = new ObservableCollection<Show>();
            SelectedShows = new ObservableCollection<object>();

            LoadShowsCommand = new Command(async () => await LoadItems());
            AddShowCommand = new Command(OnAddItem);
            ShowSelectedCommand = new Command<Show>(OnItemSelected);
            DeleteMultipleShowsCommand = new Command(async () => await DeleteShows(), () => CanDeleteShows());
            SelectionChangedCommand = new Command(DeleteMultipleShowsCommand.ChangeCanExecute);

            SwipeIsPublishCommand = new Command<Show>(async (show) => await PublishShow(show));
            SwipeIsActiveCommand = new Command<Show>(async (show) => await ActivateShow(show));
            SwipeIsSoldOutCommand = new Command<Show>(async (show) => await SoldOutShow(show));
        }

        private async Task LoadItems()
        {
            IsBusy = true;

            try
            {
                Shows.Clear();
                var shows = await DataService.Shows.GetItemsAsync();
                foreach (var show in shows)
                {
                    this.Shows.Add(show);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
            finally
            {
                IsBusy = false;
            }
        }

        public void OnAppearing()
        {
            IsBusy = true;
        }

        private async void OnAddItem(object obj)
        {
            await Shell.Current.GoToAsync(nameof(CreateShowPage));
        }

        private async void OnItemSelected(Show show)
        {
            if (show == null)
                return;

            // This will push the ItemDetailPage onto the navigation stack
            await Shell.Current.GoToAsync($"{nameof(ShowDetailPage)}?{nameof(ShowDetailViewModel.ShowId)}={show.ShowId}");
        }

        private bool CanDeleteShows()
        {
            return SelectedShows.Count() > 0;
        }

        private async Task DeleteShows()
        {
            if (SelectedShows != null && SelectedShows.Count() > 0)
            {
                bool confirm = await App.Current.MainPage.DisplayAlert("Delete?", $"Are you sure you want to delete the selected show(s)?", "Yes", "No");
                if (confirm)
                {
                    IsBusy = true;

                    var totalDeleted = 0;
                    foreach (var showobj in SelectedShows)
                    {
                        var show = (Show)showobj;
                        var response = await DataService.Shows.DeleteItemAsync(show.ShowId.ToString());
                        if (response)
                        {
                            totalDeleted++;
                        }
                    }

                    await App.Current.MainPage.DisplayAlert("Delete Results", $"Deleted {totalDeleted} show(s).", "Ok");

                    // This will reload the page
                    IsBusy = true;
                }
            }
        }

        private async Task PublishShow(Show show)
        {
            if (show != null)
            {
                show.IsPublished = !show.IsPublished;
                await DataService.Shows.UpdateItemAsync(show);

                var message = show.IsPublished ? "published" : "unpublished";
                await App.Current.MainPage.DisplayAlert($"Show Updated!", $"Show: {show.ShowTitle} has been {message}", "Ok");
            }
        }

        private async Task ActivateShow(Show show)
        {
            if (show != null)
            {
                show.IsActive = !show.IsActive;
                await DataService.Shows.UpdateItemAsync(show);

                var message = show.IsActive ? "activated" : "archived";
                await App.Current.MainPage.DisplayAlert($"Show Updated!", $"Show: {show.ShowTitle} has been {message}", "Ok");
            }
        }

        private async Task SoldOutShow(Show show)
        {
            if (show != null)
            {
                show.IsSoldOut = !show.IsSoldOut;
                await DataService.Shows.UpdateItemAsync(show);

                var message = show.IsSoldOut ? "sold out" : "not sold out";
                await App.Current.MainPage.DisplayAlert($"Show Updated!", $"Show: {show.ShowTitle} has been marked as {message}", "Ok");
            }
        }
    }
public类ShowsViewModel:BaseViewModel
{
公共observeCollection显示{get;}
公共ObservableCollection Selected显示{get;}
公共命令SelectionChangedCommand{get;}
公共命令ShowSelectedCommand{get;}
公共命令DeleteMultipleShowsCommand{get;}
公共命令AddShowCommand{get;}
公共命令LoadShowsCommand{get;}
公共命令SwipeIsPublishCommand{get;}
公共命令SwipeIsActiveCommand{get;}
公共命令SwipeIsSoldOutCommand{get;}
公共ShowsViewModel()
{
Title=“显示”;
Shows=新的ObservableCollection();
SelectedShows=新的ObservableCollection();
LoadShowsCommand=new命令(async()=>await LoadItems());
AddShowCommand=新命令(OnAddItem);
ShowSelectedCommand=新命令(已选择);
DeleteMultipleShowCommand=new命令(异步()=>await DeleteShows(),()=>CanDeleteShows());
SelectionChangedCommand=new命令(DeleteMultipleShowCommand.ChangeCanExecute);
SwipeIsPublishCommand=新命令(异步(显示)=>Wait PublishShow(显示));
SwipeIsActiveCommand=new命令(异步(显示)=>wait-ActivateShow(显示));
SwipeIsSoldOutCommand=新命令(异步(显示)=>wait SoldOutShow(显示));
}
专用异步任务LoadItems()
{
IsBusy=true;
尝试
{
Shows.Clear();
var shows=await DataService.shows.GetItemsAsync();
foreach(在显示中显示变量)
{
this.Shows.Add(show);
}
}
捕获(例外情况除外)
{
Debug.WriteLine(ex);
}
最后
{
IsBusy=false;
}
}
公开无效
{
IsBusy=true;
}
专用异步无效OnAddItem(对象obj)
{
wait Shell.Current.GoToAsync(name of(CreateShowPage));
}
已选择专用异步无效(显示)
{
if(show==null)
返回;
//这将把ItemDetailPage推送到导航堆栈上
wait Shell.Current.GoToAsync($“{nameof(showtailPage)}?{nameof(showtailviewmodel.ShowId)}={show.ShowId}”);
}
二等兵布尔·坎德莱斯豪斯()
{
返回SelectedShows.Count()>0;
}
专用异步任务DeleteShows()
{
if(SelectedShows!=null&&SelectedShows.Count()>0)
{
bool confirm=wait App.Current.MainPage.DisplayAlert(“删除?”,$”是否确实要删除所选节目?”,“是”,“否”);
如果(确认)
{
IsBusy=true;
var totalDeleted=0;
foreach(在SelectedShows中的var showobj)
{
var show=(show)showobj;
var response=await DataService.Shows.DeleteItemAsync(show.ShowId.ToString());
如果(答复)
{
全部删除++;
}
}
等待App.Current.MainPage.DisplayAlert(“删除结果“,$”已删除{totalDeleted}显示“,”确定”);
//这将重新加载页面
IsBusy=true;
}
}
}
专用异步任务PublishShow(Show Show)
{
如果(显示!=null)
{
show.IsPublished=!show.IsPublished;
等待DataService.Shows.UpdateItemAsync(show);
var message=show.IsPublished?“published”:“unpublished”;
等待App.Current.MainPage.DisplayAlert($“显示已更新!”,$“显示:{Show.ShowTitle}已被{message}”,“确定”);
}
}
专用异步任务ActivateShow(显示)
{
如果(显示!=null)
{
show.IsActive=!show.IsActive;
等待DataService.Shows.UpdateItemAsync(show);
var message=show.IsActive?“已激活”:“已存档”;
等待App.Current.MainPage.DisplayAlert($“显示已更新!”,$“显示:{Show.ShowTitle}已被{message}”,“确定”);
}
}
专用异步任务SoldOutShow(显示)
{
如果(显示!=null)
{
show.IsSoldOut=!show.IsSoldOut;
等待DataService.Shows.UpdateItemAsync(show);
var message=show.IsSoldOut?“售罄”:“未售罄”;
wait App.Current.MainPage.DisplayAlert($“Show Updated!”,$“Show:{Show.ShowTitle}已标记为{message}”,“Ok”);
}
}
}
public partial class ShowsPage : ContentPage
    {
        private ShowsViewModel _viewModel;

        public ShowsPage()
        {
            InitializeComponent();
            BindingContext = _viewModel = new ShowsViewModel();
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();
            _viewModel.OnAppearing();
        }
    }
public class ShowsViewModel : BaseViewModel
    {
        public ObservableCollection<Show> Shows { get; }
        public ObservableCollection<object> SelectedShows { get; }

        public Command SelectionChangedCommand { get; }
        public Command ShowSelectedCommand { get; }
        public Command DeleteMultipleShowsCommand { get; }
        public Command AddShowCommand { get; }
        public Command LoadShowsCommand { get; }
        public Command SwipeIsPublishCommand { get; }
        public Command SwipeIsActiveCommand { get; }
        public Command SwipeIsSoldOutCommand { get; }

        public ShowsViewModel()
        {
            Title = "Shows";
            Shows = new ObservableCollection<Show>();
            SelectedShows = new ObservableCollection<object>();

            LoadShowsCommand = new Command(async () => await LoadItems());
            AddShowCommand = new Command(OnAddItem);
            ShowSelectedCommand = new Command<Show>(OnItemSelected);
            DeleteMultipleShowsCommand = new Command(async () => await DeleteShows(), () => CanDeleteShows());
            SelectionChangedCommand = new Command(DeleteMultipleShowsCommand.ChangeCanExecute);

            SwipeIsPublishCommand = new Command<Show>(async (show) => await PublishShow(show));
            SwipeIsActiveCommand = new Command<Show>(async (show) => await ActivateShow(show));
            SwipeIsSoldOutCommand = new Command<Show>(async (show) => await SoldOutShow(show));
        }

        private async Task LoadItems()
        {
            IsBusy = true;

            try
            {
                Shows.Clear();
                var shows = await DataService.Shows.GetItemsAsync();
                foreach (var show in shows)
                {
                    this.Shows.Add(show);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
            finally
            {
                IsBusy = false;
            }
        }

        public void OnAppearing()
        {
            IsBusy = true;
        }

        private async void OnAddItem(object obj)
        {
            await Shell.Current.GoToAsync(nameof(CreateShowPage));
        }

        private async void OnItemSelected(Show show)
        {
            if (show == null)
                return;

            // This will push the ItemDetailPage onto the navigation stack
            await Shell.Current.GoToAsync($"{nameof(ShowDetailPage)}?{nameof(ShowDetailViewModel.ShowId)}={show.ShowId}");
        }

        private bool CanDeleteShows()
        {
            return SelectedShows.Count() > 0;
        }

        private async Task DeleteShows()
        {
            if (SelectedShows != null && SelectedShows.Count() > 0)
            {
                bool confirm = await App.Current.MainPage.DisplayAlert("Delete?", $"Are you sure you want to delete the selected show(s)?", "Yes", "No");
                if (confirm)
                {
                    IsBusy = true;

                    var totalDeleted = 0;
                    foreach (var showobj in SelectedShows)
                    {
                        var show = (Show)showobj;
                        var response = await DataService.Shows.DeleteItemAsync(show.ShowId.ToString());
                        if (response)
                        {
                            totalDeleted++;
                        }
                    }

                    await App.Current.MainPage.DisplayAlert("Delete Results", $"Deleted {totalDeleted} show(s).", "Ok");

                    // This will reload the page
                    IsBusy = true;
                }
            }
        }

        private async Task PublishShow(Show show)
        {
            if (show != null)
            {
                show.IsPublished = !show.IsPublished;
                await DataService.Shows.UpdateItemAsync(show);

                var message = show.IsPublished ? "published" : "unpublished";
                await App.Current.MainPage.DisplayAlert($"Show Updated!", $"Show: {show.ShowTitle} has been {message}", "Ok");
            }
        }

        private async Task ActivateShow(Show show)
        {
            if (show != null)
            {
                show.IsActive = !show.IsActive;
                await DataService.Shows.UpdateItemAsync(show);

                var message = show.IsActive ? "activated" : "archived";
                await App.Current.MainPage.DisplayAlert($"Show Updated!", $"Show: {show.ShowTitle} has been {message}", "Ok");
            }
        }

        private async Task SoldOutShow(Show show)
        {
            if (show != null)
            {
                show.IsSoldOut = !show.IsSoldOut;
                await DataService.Shows.UpdateItemAsync(show);

                var message = show.IsSoldOut ? "sold out" : "not sold out";
                await App.Current.MainPage.DisplayAlert($"Show Updated!", $"Show: {show.ShowTitle} has been marked as {message}", "Ok");
            }
        }
    }
CommandParameter="{Binding Source={RelativeSource Self}, Path=BindingContext}"
private async void PublishedSwipeItem_Invoked(object sender, System.EventArgs e)
{
  var show = (sender as SwipeItem)?.BindingContext as Show;

  if (show!= null)
    await viewModel.PublishShow(show);
}