[英]How can I dynamically change the Source of an Image within a CommandBar?
After searching for one day through the depth of the Internet, I try to ask you directly... 在搜寻了一天的互联网深度之后,我尝试直接问您...
I wrote a Windows 10 UWP App in C#. 我用C#编写了Windows 10 UWP App。 I would like to Show the strength of the wifi Connection.
我想展示wifi连接的强度。 I have several Images to show this.
我有几个图片可以显示这一点。 The wifi strength should be shown in the TopAppBar.
wifi强度应显示在TopAppBar中。
I used MVVM to set the Image Source. 我使用MVVM设置图像源。 It works fine for an Image within a UserControl but I am not able to show the Image in the CommandBar.
对于UserControl中的图像,它工作正常,但无法在CommandBar中显示该图像。
I have an Event that gives me the Uri ("Picture") of the actual Image. 我有一个事件,该事件为我提供了实际图像的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;
}
);
}
}
this is where it works fine within the UserControl: 这是在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"/>
and this is the problematic Scene: 这是有问题的场景:
<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>
any suggestions please? 有什么建议吗?
the solution is: 解决方案是:
DataTemplate Access Problems... fixed by: DataTemplate访问问题...已通过以下方法解决:
public static class DataTemplateObjects
{
public static DependencyObject FindChildControl<T>(DependencyObject control, string ctrlName)
{
int childNumber = VisualTreeHelper.GetChildrenCount(control);
for (int i = 0; i < childNumber; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(control, i);
FrameworkElement fe = child as FrameworkElement;
// Not a framework element or is null
if (fe == null) return null;
if (child is T && fe.Name == ctrlName)
{
// Found the control so return
return child;
}
else
{
// Not found it - search children
DependencyObject nextLevel = FindChildControl<T>(child, ctrlName);
if (nextLevel != null)
return nextLevel;
}
}
return null;
}
}
and then in my Event set the source of the Image: 然后在我的事件中设置图片的来源:
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;
Image img = DataTemplateObjects.FindChildControl<Image>(commandBar, "imgWifi") as Image;
if (img == null) return;
img.Source = pic;
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.