【问题标题】:Navigating from an UserControl and passing a complex object从 UserControl 导航并传递复杂对象
【发布时间】:2013-07-12 09:48:33
【问题描述】:

我很难理解导航在 WP8 上的工作原理,我正面临这个问题:

假设我在一个单独的项目中有一个自定义控件,并且我在同一个项目中有一个控件设置页面。

结构如下:

CustomControlProject
                    |- CustomControl.xaml
                    |- CustomControlSettings.xaml

CustomControl 扩展了UserControl。

现在,我要做的是将一些数据传递给CustomControlSettings.xaml,我说的是一个复杂的对象(StackPanel)。

由于CustomControl 是UserControl,我没有NavigationService,所以我正在使用此代码(我在stackoverflow 上找到了它,但我丢失了选项卡):

    /// <summary>

    /// Walk visual tree to find the first DependencyObject  of the specific type.

    /// </summary>

    private DependencyObject GetDependencyObjectFromVisualTree(DependencyObject startObject, Type type)
    {

        //Walk the visual tree to get the parent(ItemsControl)

        //of this control

        DependencyObject parent = startObject;

        while (parent != null)
        {

            if (type.IsInstanceOfType(parent))

                break;

            else

                parent = VisualTreeHelper.GetParent(parent);

        }

        return parent;

    }

这样我就可以了

Page pg = GetDependencyObjectFromVisualTree(this, typeof(Page)) as Page;
pg.NavigationService ...

传递一个复杂的对象需要其他东西,所以我按照这里的说明进行操作:http://www.sharpregion.com/easy-windows-phone-7-navigation/

这最终得到了一个像这样的导航方法:

base.OnTap(e);
Page pg = GetDependencyObjectFromVisualTree(this, typeof(Page)) as Page;
NavigationExt.Navigator.Navigate<DestinationPage>(pg.NavigationService, objectToPass);

发生的情况是我在另一个项目中使用此控件,作为MainPage.xaml 的子项。

这应该意味着Page pg = GetDependencyObjectFromVisualTree(this, typeof(Page)) as Page; 将获得MainPage 作为pg,这会导致异常,因为在与MainPage.xaml 相同的文件夹中没有DestinationPage.xaml。

    Message "No XAML was found at the location '/DestinationPage.xaml'."

所以问题是:

如果我在项目 B 中有自定义控件和页面,如果我从项目 A 中引用项目 B,我如何将对象传递到页面并导航到该页面?

【问题讨论】:

    标签: c# .net xaml navigation windows-phone-8


    【解决方案1】:

    查看您的问题和代码,我了解到您希望在用户控件中点击某些元素时导航到页面。页面和用户控件都位于与主项目不同的项目中。

    由于您的用户控件将托管在主项目的某个页面中,因此导航时的 URI 应如下所示:

    /{assemblyName};component/{relativePath}
    

    现在,NavigationService 在 UserControl 中不可用,但您可以使用 Application.RootVisual

    base.OnTap(e);
    var frame = Application.Current.RootVisual as PhoneApplicationFrame;
    frame.Navigate(new Uri("/CustomControlProject;component/CustomControlSettings.xaml",
           UriKind.Relative));
    

    对于向页面传递对象,有很多方法。其中之一是利用PhoneApplicationService。

    你可以这样做:

    base.OnTap(e);
    MyObjectType objectToPass = new MyObjectType();
    PhoneApplicationService.Current.State["myObject"] = objectToPass;
    var frame = Application.Current.RootVisual as PhoneApplicationFrame;
    frame .Navigate(new Uri("/CustomControlProject;component/CustomControlSettings.xaml",
        UriKind.Relative));
    
    // In destination page's constructor
    public CustomControlSettings() {
      var myObject = (MyObjectType) PhoneApplicationService.Current.State["myObject"];
      // ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-09
      • 2011-08-31
      • 1970-01-01
      • 2013-10-04
      • 2014-01-06
      • 2017-07-10
      相关资源
      最近更新 更多