获取和添加 Inkstrokes
getting and adding Inkstrokes
我正在尝试自己创建一个笔记应用程序。我希望该应用程序有多个页面,每个页面都有自己的 'InkCanvas'。我已经做到了这一点,但是每当我离开页面时,'InkStrokes' 的 'collection/list' 就会被清除。
我的解决方案是创建 'InkStrokes' 的列表。将该列表保存在 pages.xaml.cs 中,然后在再次导航页面时将该列表加载到 'InkCanvas' 中。
这里我尝试先获取笔画然后再将它们添加回去:
var strokes = inkCanvas.InkPresenter.StrokeContainer.GetStrokes();
foreach (var stroke in strokes)
{
inkCanvas.InkPresenter.StrokeContainer.AddStroke(stroke);
}
我也尝试过,但没有成功:
private IEnumerable<InkStroke> pageStrokes;
pageStrokes = new IEnumerable<InkStroke> //Line continues
inkCanvas.InkPresenter.StrokeContainer.GetStrokes();
inkCanvas.InkPresenter.StrokeContainer.AddStrokes(pageStrokes);
我怀疑我的问题是由于数据类型不匹配造成的。如果有人回答这个问题可以解释一些可能的解决方案及其工作原理,那将非常有帮助。
提前,非常感谢你们好心的互联网陌生人。
My solution is to create a list of 'InkStrokes'. Save that list in the pages.xaml.cs and then load that list into the 'InkCanvas' whenever the page is navigated onto again.
您可以保存 StrokeContainer
而不是保存 Strokes
。我制作了一个基本演示并将 StrokeContainer
保存到应用程序资源并将它们加载到 OnNavigatedTo
并且它有效。
MainPage.xaml.cs:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
//if the mainpage resource exists then load the container
if (Application.Current.Resources.ContainsKey("mainpage"))
{
var resource = Application.Current.Resources["mainpage"];
myCanvas.InkPresenter.StrokeContainer = (InkStrokeContainer)resource;
}
}
//I save the StrokeContainer in a button click event
private void myBtn_Click(object sender, RoutedEventArgs e)
{
Application.Current.Resources.Add(new KeyValuePair<object, object>("mainpage", myCanvas.InkPresenter.StrokeContainer));
}
这是我的演示项目:SaveStrokeSample。
我正在尝试自己创建一个笔记应用程序。我希望该应用程序有多个页面,每个页面都有自己的 'InkCanvas'。我已经做到了这一点,但是每当我离开页面时,'InkStrokes' 的 'collection/list' 就会被清除。
我的解决方案是创建 'InkStrokes' 的列表。将该列表保存在 pages.xaml.cs 中,然后在再次导航页面时将该列表加载到 'InkCanvas' 中。
这里我尝试先获取笔画然后再将它们添加回去:
var strokes = inkCanvas.InkPresenter.StrokeContainer.GetStrokes();
foreach (var stroke in strokes)
{
inkCanvas.InkPresenter.StrokeContainer.AddStroke(stroke);
}
我也尝试过,但没有成功:
private IEnumerable<InkStroke> pageStrokes;
pageStrokes = new IEnumerable<InkStroke> //Line continues
inkCanvas.InkPresenter.StrokeContainer.GetStrokes();
inkCanvas.InkPresenter.StrokeContainer.AddStrokes(pageStrokes);
我怀疑我的问题是由于数据类型不匹配造成的。如果有人回答这个问题可以解释一些可能的解决方案及其工作原理,那将非常有帮助。
提前,非常感谢你们好心的互联网陌生人。
My solution is to create a list of 'InkStrokes'. Save that list in the pages.xaml.cs and then load that list into the 'InkCanvas' whenever the page is navigated onto again.
您可以保存 StrokeContainer
而不是保存 Strokes
。我制作了一个基本演示并将 StrokeContainer
保存到应用程序资源并将它们加载到 OnNavigatedTo
并且它有效。
MainPage.xaml.cs:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
//if the mainpage resource exists then load the container
if (Application.Current.Resources.ContainsKey("mainpage"))
{
var resource = Application.Current.Resources["mainpage"];
myCanvas.InkPresenter.StrokeContainer = (InkStrokeContainer)resource;
}
}
//I save the StrokeContainer in a button click event
private void myBtn_Click(object sender, RoutedEventArgs e)
{
Application.Current.Resources.Add(new KeyValuePair<object, object>("mainpage", myCanvas.InkPresenter.StrokeContainer));
}
这是我的演示项目:SaveStrokeSample。