C# GetFileAsync然后通过反射调用Windows.System.Launcher.LaunchFileAsync
我正在开发一个windows phone应用程序,目标是7.1,因此它可以在wp7和wp8设备上运行。 如果应用程序在wp8设备上运行,我想运行以下代码:C# GetFileAsync然后通过反射调用Windows.System.Launcher.LaunchFileAsync,c#,reflection,windows-phone-8,C#,Reflection,Windows Phone 8,我正在开发一个windows phone应用程序,目标是7.1,因此它可以在wp7和wp8设备上运行。 如果应用程序在wp8设备上运行,我想运行以下代码: public async void DefaultLaunch2a() { // Path to the file in the app package to launch var file1 = await ApplicationData .Current .LocalFolder
public async void DefaultLaunch2a()
{
// Path to the file in the app package to launch
var file1 = await ApplicationData
.Current
.LocalFolder
.GetFileAsync("webcam-file.jpg");
if (file1 != null)
{
// Launch the retrieved file
var success = await Windows.System.Launcher.LaunchFileAsync(file1);
if (success)
{
// File launched/y
}
else
{
// File launched/n
}
}
else
{
// Could not find file
}
}
启动程序文件类型(用于打开图像)。我试着通过思考来做,但我有一些问题
String name = "file1.jpg";
Type taskDataType2 = Type.GetType("Windows.Storage.StorageFolder, Windows, "
+ "Version=255.255.255.255, Culture=neutral, "
+ "PublicKeyToken=null, "
+ "ContentType=WindowsRuntime");
MethodInfo showmethod2 = taskDataType2.GetMethod("GetFileAsync",
new Type[]
{
typeof(System.String)
});
showmethod2.Invoke(taskDataType2,
new System.String[] { name });
此代码引发异常TargetException:对象与目标类型不匹配
-当我调用该方法时
怎么了?是否有人已经尝试过使用反射编写上面的代码?
目标是从设备存储读取图像文件,然后启动Windows.System.Launcher.LaunchFileAsync
。
如果代码在wp8设备上运行,我想执行类似mangopollo的操作。问题在于,您应该在
taskDataType2
的实例上调用该方法,而不是在表示类型的对象上调用taskDataType2
不是Windows.Storage.StorageFolder的实例,它是类型的实例。试着这样做:
Type taskDataType2
= Type.GetType("Windows.Storage.StorageFolder, Windows,"
+ " Version=255.255.255.255, Culture=neutral,"
+ " PublicKeyToken=null, ContentType=WindowsRuntime");
MethodInfo showmethod2 = taskDataType2
.GetMethod("GetFileAsync", new[] { typeof(string) });
object taskDataInstance = taskDataType2
.GetConstructor(Type.EmptyTypes)
.Invoke(null);
String name = "file1.jpg";
showmethod2.Invoke(taskDataInstance, new[] { name });
这被简化为可以使用无参数构造函数实例化类型的情况。否则,应该使用适当的参数而不是Type.EmptyTypes
调用
请注意,这不是检索StorageFolder
实例的方法:
通常,通过异步方法和/或函数调用访问StorageFolder
对象。例如,静态方法GetFolderFromPathAsync
返回表示指定文件夹的StorageFolder
问题在于,您应该在taskDataType2
的实例上调用该方法,而不是在表示该类型的对象上调用该方法taskDataType2
不是Windows.Storage.StorageFolder
的实例,它是类型的实例。试着这样做:
Type taskDataType2
= Type.GetType("Windows.Storage.StorageFolder, Windows,"
+ " Version=255.255.255.255, Culture=neutral,"
+ " PublicKeyToken=null, ContentType=WindowsRuntime");
MethodInfo showmethod2 = taskDataType2
.GetMethod("GetFileAsync", new[] { typeof(string) });
object taskDataInstance = taskDataType2
.GetConstructor(Type.EmptyTypes)
.Invoke(null);
String name = "file1.jpg";
showmethod2.Invoke(taskDataInstance, new[] { name });
这被简化为可以使用无参数构造函数实例化类型的情况。否则,应该使用适当的参数而不是Type.EmptyTypes
调用
请注意,这不是检索StorageFolder
实例的方法:
通常,通过异步方法和/或函数调用访问StorageFolder
对象。例如,静态方法GetFolderFromPathAsync
返回表示指定文件夹的StorageFolder