Universal Windows Platform 下拉更新(使用NuGet包)

除了使用ScrollViewer实现下来更新,也可以使用第三方的NuGet包实现该效果。
NuGet包名:PullToRefresh.UWP.
Github: https://github.com/MS-UAP/PullToRefresh.UWP

Xaml code:

 xmlns:pr="using:PullToRefresh.UWP"//先添加命名空间

<!--将listView放在PullToRefreshBox中-->
 <pr:PullToRefreshBox x:Name="pr" RefreshInvoked="PullToRefreshBox_RefreshInvoked">
            <ListView x:Name="lsitView" ItemsSource="{x:Bind Items}" />
        </pr:PullToRefreshBox>

CS code:

public ObservableCollection<object> Items { get; set; }

//实现PullToRefreshBox_RefreshInvoked事件,注意参数类型
 private void PullToRefreshBox_RefreshInvoked(DependencyObject sender,object e)
        {
            if (Items.Count > 7)
            {
                Items.Clear();
            }
            else
            {
                Random r = new Random();
                Items.Insert(0, r.Next());
            }
        }
Advertisements

Xamarin开发,使用MessageCenter在ViewModel和View之间传递值

最近在学习Xamarin.Forms的开发,并且使用MVVM的结构。在学习的过程中发现页面的点击命令都在ViewModel中的Command实现,如果想要点击按键之后实现页面的跳转,必须在UI层调用 Navigation.Push方法才能实现。后来想想可以用MessageCenter实现。下面是实现方法:

首先定义一个传递的数据类,

public class NavigationMessage  
{  
    public object Paremeter { get; set; }  
}  

在ViewModel中输入发现送消息代码,可放在某个点击命令中,

MessagingCenter.Send<NavigationMessage>(new NavigationMessage() { Paremeter = "hello" },  "Test");  

在View中订阅该消息通知,

protected override void OnAppearing()  
{  
    SubscribeToMessages();  
    base.OnAppearing();  
}  
  
protected override void OnDisappearing()  
{  
    UnsubscribeToMessages();  
    base.OnDisappearing();  
}  
  
private void SubscribeToMessages()  
{  
    MessagingCenter.Subscribe<NavigationMessage>(this, "Test", (args) => {  
        Navigation.PushAsync(new TestPage());//跳转到新的页面  
    });  
}  
  
private void UnsubscribeToMessages()  
{  
    MessagingCenter.Unsubscribe<NavigationMessage>(this, "Test");  
}  

Universal Windows Platform 下拉更新

Xmal Code:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        
        <ScrollViewer x:Name="scrollViewer"
                     Loaded="scrollViewer_Loaded"
                      ViewChanged="scrollViewer_ViewChanged">

            <StackPanel Orientation="Vertical">
                <ProgressRing IsActive="{x:Bind IsPullRefresh,Mode=OneWay}" Height="30"></ProgressRing>
                <ListView x:Name="list" ItemsSource="{x:Bind Items}" ></ListView>
            </StackPanel>
            
        </ScrollViewer>
    </Grid>

CS code:

 public sealed partial class MainPage : Page, INotifyPropertyChanged
    {
        public ObservableCollection<object> Items { get; set; }

        public bool IsPullRefresh
        {
            get
            {
                return _isPullRefresh;
            }

            set
            {
                _isPullRefresh = value;
                OnPropertyChanged(nameof(IsPullRefresh));
            }
        }

        bool _isPullRefresh = false;

        public MainPage()
        {
            this.InitializeComponent();

            Items = new ObservableCollection<object>();
            for (int i = 0; i < 40; i++)
            {
                Items.Add(i);
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string name)
        {
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }

        private void scrollViewer_Loaded(object sender, RoutedEventArgs e)
        {
            scrollViewer.ChangeView(null, 30, null);
        }

        private async void scrollViewer_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
        {
            var sv = sender as ScrollViewer;

            if (!e.IsIntermediate)
            {
                if (sv.VerticalOffset == 0.0)
                {
                    IsPullRefresh = true;
                    await Task.Delay(2000);
                    for (int i = 0; i < 5; i++)
                    {
                        Items.Insert(0, i);
                    }
                    sv.ChangeView(null, 30, null);
                }
                IsPullRefresh = false;
            }
        }
    }