当我在窗体上迭代一堆不同的控件时,而不是尝试访问Text属性:
String text = String.Empty;
foreach(Control control in this.Controls)
{
try
{
text = control.Text;
}
catch(Exception exception)
{
// This control probably doesn't have the Text property.
Debug.WriteLine(exception.Message);
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法确定给定的控件是否具有Text属性?像这样的东西:
String text = String.Empty;
foreach(Control control in this.Controls)
{
if(control has Text property)
{
text = control.Text;
}
}
Run Code Online (Sandbox Code Playgroud)
我绝对鄙视Try/Catch块(除非当然没有更好的选择).
我在Windows 8商店中有一个Windows运行时应用程序,并希望在我的新(和第一个)Windows Phone 8手机应用程序中使用一些类似的功能.
ListView
Windows Phone 8 是原生的吗?或者我们可以ListView
在Windows Phone 8应用程序中使用?我在工具箱中找不到一个.
如果没有ListView
,是否可以有某种列表,允许水平方向(意思是,所有列表项都是水平呈现 - 而不是自上而下)?
那FlipView控件怎么样?我们可以在Windows Phone应用程序中使用Windows 8 Metro FlipView控件吗?
我试图使用以下方法选择一个文件:
private async void Button_Click_1(object sender, RoutedEventArgs e)
{
try
{
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".jpeg");
openPicker.FileTypeFilter.Add(".png");
StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
// Application now has read/write access to the picked file
txt.Text = "Picked file: " + file.Name;
}
else
{
txt.Text = "Operation cancelled.";
}
}
catch (Exception exception)
{
txt.Text = exception.Message;
}
}
Run Code Online (Sandbox Code Playgroud)
...但它抛出一个异常:`不支持指定的方法.";
我复制并粘贴了Windows Phone 8文档中的代码.他们的样本都不起作用.我想也许我错过了一个文档功能/合同或其他什么,但它们甚至不存在于VS for Phone应用程序中.
为什么这不起作用?
我已将其追踪到尝试的第一行:
FileOpenPicker openPicker …
Run Code Online (Sandbox Code Playgroud)