我想为我的WP8应用程序创建一个函数,它告诉用户GPS信号是否正在准备就绪,准备等等。我想用小图片来做,如果还在搜索则是问号,如果没有数据则是X,以及如果确定,请勾选。我正在使用MVVM Light。
这是我到目前为止所做的:
在我的视图中,我有以下几行:
...
DataContext="{Binding Signal, Source={StaticResource Locator}}">
...
<Image Source="{Binding Signalstrength.Imageuri, Converter={StaticResource SignalstatusConverter}}" Width="38" Height="38" HorizontalAlignment="Right" Grid.Column="1" />
我创建了一个转换器:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string path = System.Convert.ToString(value);
if (App.Geolocator != null)
{
switch (App.Geolocator.LocationStatus)
{
case Windows.Devices.Geolocation.PositionStatus.Disabled:
path = "/Assets/close.png";
break;
case Windows.Devices.Geolocation.PositionStatus.Initializing:
path = "/Assets/questionmark.png";
break;
case Windows.Devices.Geolocation.PositionStatus.NoData:
path = "/Assets/close.png";
break;
case Windows.Devices.Geolocation.PositionStatus.NotAvailable:
path = "/Assets/close.png";
break;
case Windows.Devices.Geolocation.PositionStatus.NotInitialized:
path = "/Assets/questionmark.png";
break;
case Windows.Devices.Geolocation.PositionStatus.Ready:
path = "/Assets/check.png";
break;
default:
path = "/Assets/close.png";
break;
}
}
else
{
path = "/Assets/questionmark.png";
}
return path;
}
在ViewModel类中:
public class SignalViewModel : ViewModelBase
{
private Signalstrength _signalstrength;
public Signalstrength Signalstrength
{
get { return _signalstrength; }
set
{
_signalstrength = value;
Set("Signalstrength", ref _signalstrength, value);
}
}
public SignalViewModel()
{
_signalstrength = new Signalstrength
{
Imageuri = "/Assets/questionmark.png",
State = "initializing"
};
}
}
型号:
public class Signalstrength : ObservableObject
{
private string _state;
private string _imageuri;
public string State
{
get { return _state; }
set
{
_state = value;
Set("State", ref _state, value);
}
}
public string Imageuri
{
get { return _imageuri; }
set
{
_imageuri = value;
Set("Imageuri", ref _imageuri, value);
}
}
}
当我运行应用程序时,它工作正常,但只有一次。如果我在我的主页中启动Geolocator,我发现它找到了我的位置,因此状态必须正常,但图像仍然是问号图像,而不是复选标记。
我错过了什么?如何刷新图像?最后,我尝试实现的方法符合MVVM要求吗?
修改 我不知道这是否有问题,但我有3个ViewModel:MainViewModel,WorkoutViewModel和SignalViewModel。在我的MainPage中,我显示如下:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<views:WorkoutView IsHitTestVisible="False" />
</Grid>
<Grid Grid.Row="2">
<views:SignalstrengthView IsHitTestVisible="False" />
</Grid>
我在他们的视图中设置了DataContext。 MainViewModel用于MainPage,我将它用于Applicationbar。如果用户点击AppbarButton,我会打开GPS。因此代码在MainViewModel中,但显示这些更改的项目在SignalViewModel中。这可能是个问题吗?
EDIT2: 似乎Geolocator在后台线程中运行,因此我无法更新UI。
答案 0 :(得分:0)
这是你的新模特
public class Signalstrength : ObservableObject
{
private PositionStatus _state;
public PositionStatus State
{
get { return _state; }
set
{
_state = value;
Set("State", ref _state, value);
}
}
}
在视图模型的构造函数中
_signalstrength = new Signalstrength
{
State = Windows.Devices.Geolocation.PositionStatus.Disabled
};
这是视图
...
DataContext="{Binding Signal, Source={StaticResource Locator}}">
...
<Image Source="{Binding Signalstrength.State, Converter={StaticResource SignalstatusConverter}}" Width="38" Height="38" HorizontalAlignment="Right" Grid.Column="1" />