0
经过深入的互联网搜索一天后,我试着直接问你...如何动态更改CommandBar中图像的来源?
我在C#中编写了一个Windows 10 UWP应用程序。我想展示无线连接的实力。我有几张图片来展示这一点。 WiFi强度应该显示在TopAppBar中。
我用MVVM设置图像源。它适用于UserControl中的图像,但我无法在CommandBar中显示图像。
我有一个事件给我的实际图像的Uri(“图片”)。
private void WiFiInformationUpdated(object sender, WiFiInformationEventArgs args)
{
if (args.SSID != _viewModel.WifiInformationData.SSID)
{
_viewModel.WifiInformationData.SSID = args.SSID;
}
if (args.SignalBars != _viewModel.WifiInformationData.SignalBars)
{
_viewModel.WifiInformationData.SignalBars = args.SignalBars;
}
if(args.Picture != null)
{
Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,() =>
{
var pic = new BitmapImage(args.Picture);
_viewModel.WifiInformationData.Picture = pic;
}
);
}
}
这是它的用户控件内正常工作:
<UserControl.DataContext>
<Binding Path="Main" Source="{StaticResource Locator}"/>
</UserControl.DataContext>
<Image Source="{Binding WifiInformationData.Picture}" Grid.Row="1" Grid.Column="2" DataContextChanged="Image_DataContextChanged"/>
,这是有问题的场景:
<Page.DataContext>
<Binding Path="Main" Source="{StaticResource Locator}"/>
</Page.DataContext>
<Page.TopAppBar>
<CommandBar HorizontalContentAlignment="Center" IsOpen="True" IsSticky="True" CompositeMode="Inherit">
<CommandBar.ContentTemplate>
<DataTemplate>
<RelativePanel VerticalAlignment="Stretch" Width="200" >
<Image Source="{Binding Main.WifiInformationData.Picture, Source={StaticResource Locator}}" Width="20" RelativePanel.Below="tbPercentWifi" DataContextChanged="Image_DataContextChanged"/>
</RelativePanel>
</DataTemplate>
</CommandBar.ContentTemplate>
</CommandBar>
</Page.TopAppBar>
有什么建议吗?
你试过' UpdateLayout()'或使布局无效? –
不,我没有,但是...我自己找到了解决方案:-) –