在一些要用到网络连接的应用中,应该在应用启动时先判断是否有网络连接,如果有就更新数据,如果没有网络连接,就显示提示信息。就像twitter客户端那样。
首先加入命名空间
using System.Net.NetworkInformation;
判断网络连接的代码:
if (NetworkInterface.GetIsNetworkAvailable())
{ 有网络连接的代码 }
else { 提示用户打开网络连接,或者读取之前客户端存储的数据}
这段代码中使用到了NetworkInterface这个Class中的GetIsNetworkAvailable()方法;
具体可参考msdn:
NetworkInterface http://msdn.microsoft.com/en-us/library/k056bfdz(v=VS.95).aspx
System.Net.NetworkInformationhttp://msdn.microsoft.com/en-us/library/system.net.networkinformation(v=VS.95).aspx
在WP7的picture hub中,选中一张图片,查看图片时,点击“…”菜单,点extras…时,会出现一个菜单(这个菜单中就是可以对选中的图片进行分享或者处理的应用列表,参见http://mxmxm.com/?page_id=174),如何实现这个一键分享功能:
1 在你的应用中新建一个文件Extras.xml;

2 打开MainPage.xaml.cs,添加以下的namespace
using System.Windows.Media.Imaging;
using Microsoft.Phone; using System.IO;
using Microsoft.Xna.Framework.Media;
using System.Windows.Navigation;
3 通过以上两步,你的应用就会出现在extras…中了,但是如何处理选中的图片呢,实际上,从extras…中启动你的应用时,图片会通过导航参数“token”传递给你的应用程序.得到该图片示例如下:
if (queryStrings.ContainsKey("token"))
{
MediaLibrary library = new MediaLibrary();
Picture picture = library.GetPictureFromToken(queryStrings["token"]);
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(picture.GetImage());
WriteableBitmap picLibraryImage = new WriteableBitmap(bitmap);
//选中的图片就存在picLibrayImage中了,根据你的需要来处理picLibrayImage就行了
}
4 实际上,你的应用默认可能启动的是MainPage.xmal页面,如果从extras…菜单中启动时,你可能需要启动另一个页面(假设是PicShare.xmal)来直接分享或者处理选中的图片,如何实现呢?你可以在App.xaml.cs中判断是否存在token导航参数,如果存在启动PicaShare.xmal页面,如果不存在token参数就启动MainPage.xmal页面,你可以参考这篇文章http://mxmxm.com/?p=217示例代码如下:
public void RootFrame_Navigating(object sender, NavigatingCancelEventArgs e)
{
bool hastoken=false;
if (e.Uri.ToString().Contains("/MainPage.xaml") != true || hastoken)
return;
e.Cancel = true;
RootFrame.Dispatcher.BeginInvoke(delegate
{ if (e.Uri.ToString().Contains("token"))
{ hastoken=true;RootFrame.Navigate(new Uri("/PicShare.xaml", UriKind.Relative));}
//导航到PicShare.xaml时别忘了把token这个参数传递过去
else RootFrame.Navigate(new Uri("MainPage.xaml", UriKind.Relative));
});
}
程序运行后先判断是否已经保存了账号,如果没有保存账号就显示登录页面.如果有账号就自动登录跳转到主界面
。这样用户按back后就不会显示登录页面了.
打开App.xaml.cs
在public partial class App : Application中加入
bool islogin = false;
修改RootFrame_Navigating(object sender, NavigatingCancelEventArgs e);
public void RootFrame_Navigating(object sender, NavigatingCancelEventArgs e)
{
if (e.Uri.ToString().Contains("/MainPage.xaml") != true || islogin)
return;
e.Cancel = true;
string currentUser = "nouser";
try { currentUser = (string)userSettings["currentUser"]; }//从独立存储读取账号信息;
catch { }
RootFrame.Dispatcher.BeginInvoke(delegate
{
if (currentUser != "nouser")
{
islogin = true;
RootFrame.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));//到主界面
}
else
{
RootFrame.Navigate(new Uri("/Login.xaml", UriKind.Relative));//到登录界面
}
});
}
在private void CompleteInitializePhoneApplication中加入以下代码
RootFrame.Navigated -= CompleteInitializePhoneApplication;
当调试程序时,出现错误“The application could not be launched for debugging”,
解决方法:搞半天发现不小心把SplashScreenImage.jpg文件的Build Action改成了Compile,改回Content重新Build一下就OK了。
是到这个地方找到的方案
http://www.touchfirst.com/Blog/65-solved-windows-phone-7-error-application-could-not-be-launched-for-debugging.aspx
SOLVED: Windows Phone 7 Error “Application could not be launched for debugging”
9/25/2010 3:26:25 PM
While debugging my WP7 application, I got the following error: “Application could not be launched for debugging.
Solved it by following these steps:
1.I verified that my WP7 application wasn't installed on the target device and that there wasn't any splash screen in the project.
2.Then, uninstall the application and in Visual Studio perform the following steps:
??Add an image file with the name SplashScreenImage.jpg to the project.
??In Solution Explorer, right-click the file and select Properties.
??In the Properties pane, set Build Action to Content.
??Rebuild the application and redeploy it.