我想弄清楚如何使用MVVM光的AutoCompleteBox的最佳方式。如何在MVVM Light中使用AutoCompleteBox?
public ICommand AutoComplete
{
get
{
return new RelayCommand<KeyEventArgs>(e =>
{
var txtBox = e.OriginalSource as TextBox;
if (e.Key == Key.Unknown)
{
return;
}
string autoComplete = txtBox.Text + e.Key;
if (autoComplete.Length >= 3)
{
RestClient c = new RestClient("http://localhost:3333/api/store");
RestRequest r = new RestRequest("/GetStoreNames",Method.GET);
r.AddParameter("Name", autoComplete);
r.AddParameter("Latitude", "49");
r.AddParameter("Longitude", "49");
var d = c.BuildUri(r);
c.ExecuteAsync(r, response2 =>
{
var content = response2.Content;
});
}
});
}
}
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyUp">
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding AutoComplete, Mode=OneWay}" PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<toolkit:AutoCompleteBox x:Name="acbStore" Margin="154,196,29,0" VerticalAlignment="Top" RenderTransformOrigin="0.6,0.083" Height="162" MinimumPopulateDelay="500"/>
我做了上述,但有幾個問題。
一旦我得到結果返回如何顯示他們在自動完成區域?
我該如何延遲一次執行多個請求?正如你所看到的,我不想在輸入3個字符之前擊中服務器,但在此之後這是公平的遊戲。我有點擔心,在第一次請求返回導致浪費的帶寬之前,會向服務器發出20個請求。
雅對不起,以爲它顯示了keyDown事件,但雅我使用的。我會檢查一下,看看它是怎麼回事。 – chobo2