0
我知道在看似相同的问题上存在一堆线程,但我不能在3小时后解决这个问题,所以我真的需要一些帮助。System.UnauthorizedAccessException C#Windows 10手机模拟器
据我所知,我得到这个错误,因为系统无权访问该文件。我已经尝试将权限设置为全部和其他几个代码片段来解决我的问题,但没有任何工作。
这是使用Xaramin,
我试图填充从XML文件联系人列表框在Windows 10应用程序。我有列表框itemsSource设置为“数据上下文”和路径“myList”。 XML构建操作设置为“内容”,并将“复制到输出目录”设置为“始终复制”。
我试图按照从我的课程从初学者的教程3次,总是得到相同的错误。
错误下面是页面上的全部代码。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using System.Xml;
using System.Xml.Linq;
using Windows.Storage;
using System.Collections.ObjectModel;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace ContactsApp
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
string TEMPFILEPATH = "";
string TARGETFILEPATH = "";
private ObservableCollection<string> lstd = new ObservableCollection<string>();
public ObservableCollection<string> myList { get { return lstd; } }
public MainPage()
{
this.InitializeComponent();
}
private void Grid_Loading(FrameworkElement sender, object args)
{
Windows.ApplicationModel.Package package = Windows.ApplicationModel.Package.Current;
StorageFolder installedLocation = package.InstalledLocation;
StorageFolder targetLocation = ApplicationData.Current.LocalFolder;
TEMPFILEPATH = installedLocation.Path.ToString() + "\\Contacts.xml";
TARGETFILEPATH = targetLocation.Path.ToString() + "\\Contacts.xml";
File.Move(TEMPFILEPATH, TARGETFILEPATH);
loadContacts();
}
private void loadContacts()
{
XmlReader xmlReader = XmlReader.Create(TARGETFILEPATH);
while (xmlReader.Read())
{
if (xmlReader.Name.Equals("ID") && (xmlReader.NodeType == XmlNodeType.Element))
{
lstd.Add(xmlReader.ReadElementContentAsString());
}
}
DataContext = this;
xmlReader.Dispose();
}
}
}
我将永远感谢您对此事的任何帮助。 :)
你好,队友欢呼堡回答。我做了你所说的,现在我的代码看起来像这个https://pastebin.com/sXS4262y。我收到第49行的错误。类型不包含程序集,也没有扩展名,组件接受类型'Type'的第一个参数。 我尝试使用system.reflection添加,但没有奏效。 我也改变了xml文件设置,如你所说。 – Rick1990
忘记添加UWP中需要的'GetTypeInfo()'。编辑答案。 –
谢谢你这个工作 – Rick1990