收藏

blog地址出错。先收藏!

 

在WP7程序中判断手机是否有网络连接

在一些要用到网络连接的应用中,应该在应用启动时先判断是否有网络连接,如果有就更新数据,如果没有网络连接,就显示提示信息。就像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

发表在 Window Phone 7 | 标签为 Windows Phone7, WP7, 应用, 开发 | 3 条评论

Windows Phone7如何开发分享图片的插件

在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));
});
}

发表在 Window Phone 7 | 标签为 Picture Hub, plugin, WP7, 插件 | 4 条评论

Windows Phone7自动登录功能的实现方法

程序运行后先判断是否已经保存了账号,如果没有保存账号就显示登录页面.如果有账号就自动登录跳转到主界面
。这样用户按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;

发表在 Window Phone 7 | 标签为 Windows Phone7, WP7, 开发, 教程 | 一条评论

C# ListBox 自动滚动到底部

C# ListBox 自动滚动到底部

在ListBox中添加一条记录(ListBox.Items.Add方法)后,滚动条会自动回到顶部。我们可能更希望它自动滚动到底部,本文简要介绍几种方法。

方法一:

1
2
3
this .listBox1.Items.Add( "new line" );
this .listBox1.SelectedIndex = this .listBox1.Items.Count - 1;
this .listBox1.SelectedIndex = -1;

在添加记录后,先选择最后一条记录,滚动条会自动到底部,再取消选择。
缺点是需两次设置选中条目,中间可能会出现反色的动画,影响美观。

方法二:

1
2
this .listBox1.Items.Add( "new line" );
this .listBox1.TopIndex = this .listBox1.Items.Count - ( int )( this .listBox1.Height / this .listBox1.ItemHeight);

通过计算ListBox显示的行数,设置TopIndex属性(ListBox中第一个可见项的索引)而达到目的。

方法二 plus:智能滚动

1
2
3
4
5
6
bool scroll = false ;
if ( this .listBox1.TopIndex == this .listBox1.Items.Count - ( int )( this .listBox1.Height / this .listBox1.ItemHeight))
???????? scroll = true ;
this .listBox1.Items.Add( "new line" );
if (scroll)
???????? this .listBox1.TopIndex = this .listBox1.Items.Count - ( int )( this .listBox1.Height / this .listBox1.ItemHeight);

在添加新记录前,先计算滚动条是否在底部,从而决定添加后是否自动滚动。
既可以在需要时实现自动滚动,又不会在频繁添加记录时干扰用户对滚动条的控制。

Phone7程序无法调试错误The application could not be launched for debugging

当调试程序时,出现错误“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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值