我只是在和.Net毛伊玩。我希望从Rest服务中检索信息,并根据结果以编程方式向VerticalStackLayout添加按钮。调试解决方案时,按钮将添加到VerticalStackLayout中,但UI不会更新。
在这里,代码片段
var btn = new Button();
btn.Text = "Button " + count + " added";
btn.Clicked += OnCounterClicked;
btn.HorizontalOptions = LayoutOptions.Center;
VLayout.Add(btn);这里的XAML
<ScrollView>
<VerticalStackLayout x:Name="VLayout"
Spacing="25"
Padding="30,0"
VerticalOptions="Center">
<Image
Source="dotnet_bot.png"
SemanticProperties.Description="Cute dot net bot waving hi to you!"
HeightRequest="200"
HorizontalOptions="Center" />
<Label
Text="Hello, World!"
SemanticProperties.HeadingLevel="Level1"
FontSize="32"
HorizontalOptions="Center" />
<Label
Text="Welcome to .NET Multi-platform App UI"
SemanticProperties.HeadingLevel="Level2"
SemanticProperties.Description="Welcome to dot net Multi platform App U I"
FontSize="18"
HorizontalOptions="Center" />
<Entry x:Name="entry"
Placeholder="Enter text"
TextChanged="OnTextChanged"
Completed="OnTextCompleted" />
<Button
x:Name="KommenBtn"
Text="Kommen"
SemanticProperties.Hint="Counts the number of times you click"
Clicked="OnCounterClicked"
HorizontalOptions="Center" />
<Button
x:Name="GehenBtn"
Text="Gehen"
SemanticProperties.Hint="Counts the number of times you click"
Clicked="OnCounterClicked"
HorizontalOptions="Center" />
</VerticalStackLayout>
</ScrollView>提前感谢您的提示和帮助!带宽
发布于 2022-05-27 05:45:23
尝试更新主线程上的UI,您可以将代码包装到Application.Current.Dispatcher.Dispatch中。
样本代码
Application.Current.Dispatcher.Dispatch(() =>
{
var btn = new Button();
btn.Text = "Button " + count + " added";
btn.Clicked += OnCounterClicked;
btn.HorizontalOptions = LayoutOptions.Center;
VLayout.Add(btn);
});发布于 2022-05-21 06:37:04
若要在影响控件的层次结构(或位置)的更改后更新UI:
(VLayout as IView).InvalidateArrange();注:这大致相当于Xamarin.Forms layout.ForceLayout();
如果尚未在UI MainThread上运行的代码中,请将其包装在Dispatch中:
Dispatcher.Dispatch(() =>
(VLayout as IView).InvalidateArrange());https://stackoverflow.com/questions/72327021
复制相似问题