首先,我道歉爲標題...我還沒有發現任何相適應我的一個案例:P異步函數使代碼不工作
我需要下載一個文件INI
填補Dictionary
首先的。對於這一點,我有這個類:
public class Properties : INotifyPropertyChanged
{
private Dictionary<string, string> _properties;
public Properties()
{
_properties = new Dictionary<string, string>();
}
public async void Load(string uri)
{
Stream input = await connection(uri);
StreamReader rStream = new StreamReader(input);
string line;
while((line = rStream.ReadLine()) != null)
{
if(line != "")
{
int pos = line.IndexOf('=');
string key = line.Substring(0, pos);
string value = line.Substring(pos + 1);
_properties.Add(key, value);
Debug.WriteLine("Key: " + key + ", Value: " + value);
}
}
Debug.WriteLine("Properties dictionary filled with " + _properties.Count + " items.");
}
public async Task<Stream> connection(string uri)
{
var httpClient = new HttpClient();
Stream result = Stream.Null;
try
{
result = await httpClient.GetStreamAsync(uri);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
Debug.WriteLine(ex.HResult);
}
return result;
}
public string getValue(string key)
{
string result = "";
try
{
result = _properties[key];
}
catch(Exception ex)
{
Debug.WriteLine(ex.Message);
Debug.WriteLine(ex.HResult);
result = "Not found";
}
return result;
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged([CallerMemberName]string propertyName = "")
{
var Handler = PropertyChanged;
if (Handler != null)
Handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
主要的Dictionary
包含Key
和URL
到XML
文件下載到應用程序的每個頁面。
的MainPage
,這是一個是要去填充有這樣的代碼:
public MainPage()
{
this.InitializeComponent();
//Properties dictionary filling
prop = new Properties();
prop.Load("URL");
tab = new Bars.TopAppBar();
bab = new Bars.BottomAppBar();
tABar = this.topAppBar;
actvt = this.Activity;
bABar = this.bottomAppBar;
//Constructor of the UserControl
act = new Home(this, prop);
}
的UserControl
的構造函數使用MainPage
爲Callback
和類Properties
去尋找URL下載XML
文件。
會發生什麼情況是,作爲Properties.Load()
是一種異步方法,被調用,然後將剩餘的線被執行,並且程序完成時,然後回到Load()
並填充Dictionary
。 由於Home
構造函數依賴於Value
的Properties
我得到一個Exception
。
我試圖創建async void
s分配不同的優先級來強制程序運行第一件事,然後運行其餘的,但它也沒有工作。
因此,總結一下,我需要確保Properties
填充在首位,任何人都知道如何做到這一點?
在此先感謝!
非常感謝@ razor118!我終於解決了這個問題:) –