如何解决Xamarin表单:无法在UI上更新自定义控件
我创建了一个自定义控件,该控件具有水平条,基于我从API获得的数量,条中可以有N个框视图,例如,我有一个控件的限制为20个控件,我已经选择了10个,所以10个boxview将为橙色,其余20个中的10个将为黑色 我需要更新每个复选框的选择控制权,它对我来说一直很好,直到我转到下一个屏幕,并回到同一个屏幕,一旦我回到该屏幕,控件将停止更新,同时在代码中获取数据,但不是在用户界面上更新。
我如何在代码中使用它
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(){
FILE * fr = fopen("file.csv","r");
char save[500],line[200],to_find[50];
int oneByOne = 0;
printf("Enter the ID card number: ");
scanf("%s",to_find);
// fgets(to_find,50,stdin);
if(isdigit(to_find) && strlen(to_find) == 13){
while(fgets(line,200,fr)){
char *word = strtok(line,"\n");
strcpy(save,line);
if (strstr(save,to_find)){
char *wordone = strtok(save,",");
while (wordone != NULL){
printf("Here are your details: %s\n",wordone);
wordone = strtok(NULL,");
}
}
}
fclose(fr);
}
else { printf("enter correclty"); }
return 0;
}
我的CustomControl XAML
<views:CustomDashControl
x:Name="customDash"
Grid.Row="2"
Grid.ColumnSpan="3"
HorizontalOptions="FillAndExpand"
ItemsSource="{Binding ControlData}"
VerticalOptions="Start" />
我的自定义控件背后的代码
<ContentView
x:Class="LIRAppV1.Controls.CustomDashControl"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<StackLayout
Padding="1"
BackgroundColor="White"
HorizontalOptions="CenterAndExpand"
VerticalOptions="FillAndExpand">
<!--<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>-->
<Grid
x:Name="progressbar"
BackgroundColor="Transparent"
ColumnSpacing="0"
HorizontalOptions="FillAndExpand"
VerticalOptions="Start">
<Grid.RowDefinitions>
<RowDefinition Height="20" />
</Grid.RowDefinitions>
</Grid>
</StackLayout>
</ContentView>
解决方法
OnAppearing将在屏幕出现之前执行,如果您希望在屏幕出现之后立即执行Loaded事件,则有一种解决方法。
在如下视图模型中创建一个属性。
private bool _toggleTemp;
public bool ToggleTemp
{
get => _toggleTemp;
set => SetProperty(ref _toggleTemp,value);
}
将以下行添加到构造函数的最后一行。
LoadingVm.ToggleTemp = true;
在屏幕上添加一个开关,如下所示将IsVisible设置为false。
<Switch IsToggled="{Binding ToggleTemp}" Toggled="Switch_OnToggled" IsVisible="False" />
现在,您可以在Switch_OnToggled的“页面加载”中编写要编写的代码。
private async void Switch_OnToggled(object sender,ToggledEventArgs e)
{
/* Your code goes here... */
}
谢谢!
如果这不起作用,请参考此-
在出现的过程中,我将所有内容包装在一个异步调用中,并等待了几百毫秒以使页面呈现。
await WaitAndExecute(1000,() =>
{
// refresh code
}
protected async Task WaitAndExecute(int milisec,Action actionToExecute)
{
await Task.Delay(milisec);
actionToExecute();
}
https://forums.xamarin.com/discussion/22561/page-loaded-event
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。