每 x 秒刷新 xaml 页面并保持当前扩展器状态
Refresh xaml page every x seconds and keep the current expander state
我有一个 xamarin 项目。有一个带有扩展器列表的滚动视图。
我喜欢每 x 秒刷新一次页面,但保持扩展器的状态(isExpanded 布尔值)。
如何检查扩展器(或标签、按钮等)的状态并在每隔 x 秒刷新一次时保持这些值?
我觉得我必须向我的后台代码函数添加一个参数,类似于点击或点击事件期间的 'object sender'。
在后台代码中,我尝试使用
每 x 秒刷新一次页面
Device.StartTimer(TimeSpan.FromSeconds(x),Updatefunction);
目前它们在页面刷新时都有默认的 isExpanded (false) 状态。
您可以在 viewModel 中添加一个 bool 属性,然后将此 属性 绑定到 <Expander>
选项卡中的 IsExpanded="{Binding Expand1Opened}"
。当用户单击 Expander
时,IsExpanded 将取决于 Expand1Opened
属性 的值。无论每 x 秒刷新一次页面,它都会保持当前的扩展器状态。我为 Expander
添加 Command
,如果单击 Expander
,Expand1Opened
属性 的值将在 ViewModel 中更改。
<RefreshView IsRefreshing="{Binding IsRefreshing}"
RefreshColor="Teal"
Command="{Binding RefreshCommand}">
<ScrollView>
<StackLayout>
<Expander IsExpanded="{Binding Expand1Opened}" Command="{Binding Expand1OpenedCommand}">
<Expander.Header>
<Label Text="List1"
FontAttributes="Bold"
FontSize="Medium" />
</Expander.Header>
<Grid Padding="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<FlexLayout Direction="Row"
Wrap="Wrap"
AlignItems="Center"
AlignContent="Center"
BindableLayout.ItemsSource="{Binding Items}"
BindableLayout.ItemTemplate="{StaticResource ColorItemTemplate}" />
</Grid>
</Expander>
<Expander IsExpanded="{Binding Expand2Opened}" Command="{Binding Expand2OpenedCommand}">
<Expander.Header>
<Label Text="List2"
FontAttributes="Bold"
FontSize="Medium" />
</Expander.Header>
<Grid Padding="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<FlexLayout Direction="Row"
Wrap="Wrap"
AlignItems="Center"
AlignContent="Center"
BindableLayout.ItemsSource="{Binding Items}"
BindableLayout.ItemTemplate="{StaticResource ColorItemTemplate}" />
</Grid>
</Expander>
</StackLayout>
</ScrollView>
</RefreshView>
这是视图模型。我有两个 <Expander>
,所以我添加了两个属性 Expand1Opened
和 Expand2Opened
,并添加了两个名为 Expand1OpenedCommand
和 Expand2OpenedCommand
的 Command
,如果<Expander>
被点击,Expand1OpenedCommand
会被调用,然后Expand1Opened
的值会改变,如果refreshview被刷新,Expand1Opened
的值不会改变,所以expander
的状态将被保留。
public class MainPageViewModel : INotifyPropertyChanged
{
const int RefreshDuration = 2;
int itemNumber = 1;
readonly Random random;
bool isRefreshing;
public bool IsRefreshing
{
get { return isRefreshing; }
set
{
isRefreshing = value;
OnPropertyChanged();
}
}
bool expand1Opened = false;
public bool Expand1Opened
{
get { return expand1Opened; }
set
{
expand1Opened = value;
OnPropertyChanged();
}
}
bool expand2Opened=false;
public bool Expand2Opened
{
get { return expand2Opened; }
set
{
expand2Opened = value;
OnPropertyChanged();
}
}
public ObservableCollection<Item> Items { get; private set; }
public ICommand RefreshCommand => new Command(async () => await RefreshItemsAsync());
public ICommand Expand1OpenedCommand { get; set; }
public ICommand Expand2OpenedCommand { get; set; }
public MainPageViewModel()
{
random = new Random();
Items = new ObservableCollection<Item>();
Expand1OpenedCommand = new Command((() =>
{
expand1Opened = !expand1Opened;
}));
Expand2OpenedCommand = new Command((() =>
{
expand2Opened = !expand2Opened;
}));
AddItems();
}
void AddItems()
{
for (int i = 0; i < 1; i++)
{
Items.Add(new Item
{
Color = Color.FromRgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)),
Name = $"Item {itemNumber++}",
Isfavourite = false
});
}
}
async Task RefreshItemsAsync()
{
IsRefreshing = true;
await Task.Delay(TimeSpan.FromSeconds(RefreshDuration));
AddItems();
IsRefreshing = false;
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}
这是运行 GIF。
我有一个 xamarin 项目。有一个带有扩展器列表的滚动视图。
我喜欢每 x 秒刷新一次页面,但保持扩展器的状态(isExpanded 布尔值)。
如何检查扩展器(或标签、按钮等)的状态并在每隔 x 秒刷新一次时保持这些值?
我觉得我必须向我的后台代码函数添加一个参数,类似于点击或点击事件期间的 'object sender'。
在后台代码中,我尝试使用
每 x 秒刷新一次页面Device.StartTimer(TimeSpan.FromSeconds(x),Updatefunction);
目前它们在页面刷新时都有默认的 isExpanded (false) 状态。
您可以在 viewModel 中添加一个 bool 属性,然后将此 属性 绑定到 <Expander>
选项卡中的 IsExpanded="{Binding Expand1Opened}"
。当用户单击 Expander
时,IsExpanded 将取决于 Expand1Opened
属性 的值。无论每 x 秒刷新一次页面,它都会保持当前的扩展器状态。我为 Expander
添加 Command
,如果单击 Expander
,Expand1Opened
属性 的值将在 ViewModel 中更改。
<RefreshView IsRefreshing="{Binding IsRefreshing}"
RefreshColor="Teal"
Command="{Binding RefreshCommand}">
<ScrollView>
<StackLayout>
<Expander IsExpanded="{Binding Expand1Opened}" Command="{Binding Expand1OpenedCommand}">
<Expander.Header>
<Label Text="List1"
FontAttributes="Bold"
FontSize="Medium" />
</Expander.Header>
<Grid Padding="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<FlexLayout Direction="Row"
Wrap="Wrap"
AlignItems="Center"
AlignContent="Center"
BindableLayout.ItemsSource="{Binding Items}"
BindableLayout.ItemTemplate="{StaticResource ColorItemTemplate}" />
</Grid>
</Expander>
<Expander IsExpanded="{Binding Expand2Opened}" Command="{Binding Expand2OpenedCommand}">
<Expander.Header>
<Label Text="List2"
FontAttributes="Bold"
FontSize="Medium" />
</Expander.Header>
<Grid Padding="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<FlexLayout Direction="Row"
Wrap="Wrap"
AlignItems="Center"
AlignContent="Center"
BindableLayout.ItemsSource="{Binding Items}"
BindableLayout.ItemTemplate="{StaticResource ColorItemTemplate}" />
</Grid>
</Expander>
</StackLayout>
</ScrollView>
</RefreshView>
这是视图模型。我有两个 <Expander>
,所以我添加了两个属性 Expand1Opened
和 Expand2Opened
,并添加了两个名为 Expand1OpenedCommand
和 Expand2OpenedCommand
的 Command
,如果<Expander>
被点击,Expand1OpenedCommand
会被调用,然后Expand1Opened
的值会改变,如果refreshview被刷新,Expand1Opened
的值不会改变,所以expander
的状态将被保留。
public class MainPageViewModel : INotifyPropertyChanged
{
const int RefreshDuration = 2;
int itemNumber = 1;
readonly Random random;
bool isRefreshing;
public bool IsRefreshing
{
get { return isRefreshing; }
set
{
isRefreshing = value;
OnPropertyChanged();
}
}
bool expand1Opened = false;
public bool Expand1Opened
{
get { return expand1Opened; }
set
{
expand1Opened = value;
OnPropertyChanged();
}
}
bool expand2Opened=false;
public bool Expand2Opened
{
get { return expand2Opened; }
set
{
expand2Opened = value;
OnPropertyChanged();
}
}
public ObservableCollection<Item> Items { get; private set; }
public ICommand RefreshCommand => new Command(async () => await RefreshItemsAsync());
public ICommand Expand1OpenedCommand { get; set; }
public ICommand Expand2OpenedCommand { get; set; }
public MainPageViewModel()
{
random = new Random();
Items = new ObservableCollection<Item>();
Expand1OpenedCommand = new Command((() =>
{
expand1Opened = !expand1Opened;
}));
Expand2OpenedCommand = new Command((() =>
{
expand2Opened = !expand2Opened;
}));
AddItems();
}
void AddItems()
{
for (int i = 0; i < 1; i++)
{
Items.Add(new Item
{
Color = Color.FromRgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)),
Name = $"Item {itemNumber++}",
Isfavourite = false
});
}
}
async Task RefreshItemsAsync()
{
IsRefreshing = true;
await Task.Delay(TimeSpan.FromSeconds(RefreshDuration));
AddItems();
IsRefreshing = false;
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}
这是运行 GIF。