我是 silverlight/WPF 和 WP7 开发新手,但我熟悉使用控制台或 Windows 窗体开发 C#,所以请耐心等待!
我正在尝试使用 Silverlight SDK 和 Microsoft.Maps.MapControl 以及 System.Device.Location 类创建一个位置感知应用程序。我遇到的问题是从其他页面访问“观察者”。做这个的最好方式是什么。我想将它放在我的 GUI 页面类之外,以便在页面之间切换时,您不会检查 UI 线程上的新位置并执行重复的工作。
以下是我认为应该如何完成的概述(将 GPS 观察器移动到应用程序超类 -> 调度程序到事件更改的页面类),请有人纠正我!主要问题是如果没有实例化或静态方法,我无法访问 Page 类(MainPage.xaml 和 ListView.xaml)。如果我在页面上创建静态方法,它是无用的,因为我无论如何都无法与页面交互(例如设置位置更改的状态栏或将地图视图移动到新位置)。做这个的最好方式是什么?我曾想过只在 App 类中设置一个新的静态位置对象并从子类访问该对象,但这是手动操作。似乎调度程序是执行此操作的方法,但是有没有一种方法可以引用页面而无需实例化?
在我使用的 Windows 窗体应用程序中 -readonly FormClass form = (FormClass)Application.OpenForms["formname"];
这是我的代码的示例...
App.xaml.cs(应用程序类)- 显然这是整个 WP7 应用程序的超类,因此我将观察器放在这里。
Public Partial Class App : Application
{
Private IGeoPositionWatcher<GeoCoordinate> _watcher;
Public App()
{
//App constructor
//Mock GPS services on WP7 emulator
if (_watcher == null)
{
var events = new[] {
new GeoCoordinateEventMock { Latitude=33.43, Longitude=-112.02, Time=new TimeSpan(0,0,1) },
};
_watcher = new EventListGeoLocationMock(events);
}
//add event handlers for statuschanged and positionchanged
_watcher.StatusChanged += WatcherStatusChanged;
_watcher.PositionChanged += WatcherPositionChanged;
//start watcher
_watcher.Start();
}
#event handlers
public void WatcherStatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
Deployment.Current.Dispatcher.BeginInvoke(() => MainPage.MyStatusChanged(e));
Deployment.Current.Dispatcher.BeginInvoke(() => OtherPage.MyStatusChanged(e));
}
public void WatcherPositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
Deployment.Current.Dispatcher.BeginInvoke(() => MainPage.MyPositionChanged(e));
Deployment.Current.Dispatcher.BeginInvoke(() => OtherPage.MyPositionChanged(e));
}
}
MainPage.xaml.cs (MainPage class)-
public void MyPositionChanged(GeoPositionChangedEventArgs<GeoCoordinate> e)
{
//do some stuff
}
public void MyStatusChanged(GeoPositionStatusChangedEventArgs e)
{
//do some stuff
}
更新了-这就是我如何让它按照我需要的方式工作-
为我的位置创建了一个公共类 Loc-
public class Loc
{
private readonly GeoCoordinateWatcher _watcher;
public Location CurLoc { get; set; }
public string LocStatus { get; set; }
public Loc()
{
if (_watcher != null) return;
//---get the default accuracy-
_watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default)
{
MovementThreshold = 1000
};
//add event handlers for statuschanged and positionchanged
_watcher.StatusChanged += WatcherStatusChanged;
_watcher.PositionChanged += WatcherPositionChanged;
//start watcher
_watcher.Start();
}
private void MyPositionChanged(GeoPositionChangedEventArgs<GeoCoordinate> e)
{
//called from the _watcher Position changed eventHandler
CurLoc = new Location(e.Position.Location.Latitude, e.Position.Location.Longitude);
}
private void MyStatusChanged(GeoPositionStatusChangedEventArgs e)
{
//called from the _watcher position status changed event handler
switch (e.Status)
{
case GeoPositionStatus.Disabled:
LocStatus = "Location services disabled!";
break;
case GeoPositionStatus.Initializing:
LocStatus = "Finding current location...";
break;
case GeoPositionStatus.NoData:
LocStatus = "No location data available...";
break;
case GeoPositionStatus.Ready:
LocStatus = "Found you";
break;
}
}
private void WatcherStatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
Deployment.Current.Dispatcher.BeginInvoke(() => MyStatusChanged(e));
}
private void WatcherPositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
Deployment.Current.Dispatcher.BeginInvoke(() => MyPositionChanged(e));
}
}
然后,我在 App.xaml.cs 类中将此类实例化为静态对象,以便我可以在应用程序操作期间利用这些信息。
public partial class App
{
/// <summary>
/// Provides easy access to the root frame of the Phone Application.
/// </summary>
/// <returns>The root frame of the Phone Application.</returns>
public PhoneApplicationFrame RootFrame { get; private set; }
public static Loc AppLoc = new Loc();
然后在我的应用程序中,每当我需要访问位置信息时,我都可以使用-
App.AppLoc.CurLoc 或 App.AppLoc.LocStatus