Après une recherche d'un jour à travers la profondeur de l'Internet, j'essaie de vous demander directement ...Comment puis-je modifier dynamiquement la source d'une image dans un CommandBar?
J'ai écrit une application Windows 10 UWP en C#. Je voudrais montrer la force de la connexion wifi. J'ai plusieurs images pour le montrer. La force du wifi devrait être affichée dans le TopAppBar. J'ai utilisé MVVM pour définir la source de l'image.
Cela fonctionne correctement pour une image dans un UserControl mais je ne suis pas en mesure d'afficher l'image dans le CommandBar.
J'ai un événement qui me donne l'Uri ("Image") de l'image réelle.
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;
}
);
}
}
c'est là, il fonctionne très bien dans le UserControl:
<UserControl.DataContext>
<Binding Path="Main" Source="{StaticResource Locator}"/>
</UserControl.DataContext>
<Image Source="{Binding WifiInformationData.Picture}" Grid.Row="1" Grid.Column="2" DataContextChanged="Image_DataContextChanged"/>
et c'est la scène problématique:
<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>
suggestions s'il vous plaît?
Avez-vous essayé ' UpdateLayout() 'ou pour invalider la mise en page? –
non je n'ai pas, mais ... j'ai trouvé la solution tout seul :-) –