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()'或使佈局無效? –
不,我沒有,但是...我自己找到了解決方案:-) –