从不同的视图获取 longListSelector 源代码
本文关键字:获取 longListSelector 源代码 视图 | 更新日期: 2023-09-27 17:57:20
我正在遵循这个例子:http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj244365%28v=vs.105%29.aspx
当应用程序首次启动时,我想将某些项目添加到列表中,这些项目将无法删除。所以在 App.xaml 中.cs我有:
private void Application_Launching(object sender, LaunchingEventArgs e)
{
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
List<MyConnection.locationList.locations> source = new List<MyConnection.locationList.locations>();
if (!settings.Contains("firstrun"))
{
source.Add(new MyConnection.locationList.locations("Dulles, VA"));
source.Add(new MyConnection.locationList.locations("Dulles, VA (Q)"));
}
}
在我的位置List.xaml.cs(这是longListSelector的位置)我有:
public locationList()
{
InitializeComponent();
List<locationSelectorClass.locationChoice<locations>> DataSource = locationSelectorClass.locationChoice<locations>.CreateGroups(source,
System.Threading.Thread.CurrentThread.CurrentUICulture,
(locations s) => { return s.LastName; }, true);
}
public class locations
{
public string locName
{
get;
set;
}
public locations(string locName)
{
this.locName = locName;
}
}
显然,它声明它找不到"源",那么我如何指示它在 App.xaml 中查找.cs或者在创建后如何调用源?
我尝试过"MyConnection.App.xxxx",但它没有给我"源"选项。
如果你想使用Myconnection.App.xxxx
你必须公开源代码,如果你想获得信息,你必须写
List<MyConnection.locationList.locations> source;
private void Application_Launching(object sender, LaunchingEventArgs e)
{
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
source = new List<MyConnection.locationList.locations>();
if (!settings.Contains("firstrun"))
{
source.Add(new MyConnection.locationList.locations("Dulles, VA"));
source.Add(new MyConnection.locationList.locations("Dulles, VA (Q)"));
}
}
现在您可以获取信息了。然而,使用静力学并不是最好的方法。这意味着您可以在 locationlist() 构造函数的设置中进行搜索。或者,您可以使用导航来解析某些信息。但也许对于第一个示例,您建议的方法查看代码,或者在构造函数中加载信息将是最好的。
额外
如果要在列表中添加/删除条目,并在 GUI 中显示它们,则应使用 ObservableCollection 而不是 List。
应用程序Application_Launching
事件中,您不必执行此操作。您可以在视图/控件的构造函数中执行此操作。因此,您可以直接访问它。
不过,如果你想这样做 - 那么一旦你创建了列表 - 把它保存到IsolatedStorageSettings
,然后在你的控制中把它放回一个新对象中。在那里,您将可以访问您的列表和位置列表。如果您碰巧使用绑定,那么在控件的构造函数中,您还必须指定this.DataContext = this;
- 以便它知道在哪里查找您的位置列表。
如果您使用的是 MVVM - 那么您将指定视图模型作为您的数据上下文,并从视图模型内的IsolatedStorageSettings
中获取位置列表。