该名称已存在于树中,在Windows Phone 8.1应用程序开发中 [英] The name already exists in the tree, In Windows Phone 8.1 apps development

查看:60
本文介绍了该名称已存在于树中,在Windows Phone 8.1应用程序开发中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在VS 2013中使用WP 8.1版创建了Windows phone应用程序。

我正在使用NuGet的Windows Phone 8.1抽屉布局控件创建菜单导航控件,



1.右键单击Add Reference - >管理NuGet包 - >在搜索框中添加DrawerLayout文本。

2.选择DrawerLayout For Windows Phone 8.1并单击安装按钮。



我已经创建了用户控件,可以在所有页面中使用它来导航。

我的XAML用户设计如下 - < br $>


< usercontrol>

x:Class =Sample.Controls.DrawerNavigation

xmlns =http: //schemas.microsoft.com/winfx/2006/xaml/presentation

xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml

xmlns:dwLayout =using:DrawerLayout

xmlns:d =http://schemas.microsoft.com/expression/blend/2008

xmlns :mc =http://schemas.openxmlformats.org/markup-compatibility/2006

mc:Ignorable =d>

< grid x: name =RootLayoutxmlns:x =#unknown>

< grid.rowdefinitions>

< rowdefinition height =Auto>

< rowdefinition height =*>



<! - 标题栏 - >

< grid x:name =TitleBarremoved =Orange grid.row =0height =30>

< grid.columndefinitions>

< columndefinition width =Auto> < columndefinition width =*>



< Image x:Name =DrawerIcon

保证金=5,5,0,5

Grid.Column =0

Source =/ Assets / drawer_icon_1.png

Horizo​​ntalAlignment =Left

Tapped =DrawerIcon_Tapped

Grid.ColumnSpan =2/>

< textblock x:name =txtHeader>

Grid.Column =2

前景=白色

VerticalAlignment =中心

FontSize =15

保证金=35,0,0,-5

身高=23/>



< ! - 抽屉布局 - >

< dwlayout:drawerlayout xmlns:dwlayout =#unknown>

Grid.Row =1

x: Name =DrawerLayout

删除=透明>

<! - MainPage - >

< grid x :name =MainFragmentremoved =Transparent>



< grid x:name =ListFragmentremoved =White>

< scrollviewer>

< grid>

< grid.rowdefinitions>

< rowdefinition height =Auto >

< rowdefinition height =40>

< rowdefinition height =40>

< rowdefinition height =40>

< rowdefinition height =40>

< rowdefinition height =40>

< rowdefinition height =40>



< grid x:name =grdMenu1removed =Transparent>

Grid.Row =0Tapped =Menu1_Tapped>

< stackpanel margin =5>

Orientation =Horizo​​ntal>

< Image x:Name =imgMenu1

高度=20宽度=20

来源=ms-appx:///Assets/Menu1.png

Stretch =UniformToFill />

< textblock x:name =Menu1>

Grid.Row =0

Text = Menu1

Margin =10 0 0 0

Horizo​​ntalAlignment =Left

VerticalAlignment =Center

FontSize =12

Foreground =Black/>





< grid x:name =grdMenu2>

删除=透明Grid.Row =1Tapped =Menu2_Tapped>

< stackpanel margin =5orientation =Horizo​​ntal>

< Image x:Name =imgMenu2Height =20Width =20Source = ms-appx:///Assets/Menu2.pngStretch =UniformToFill/>

< textblock x:name =Menu2grid.row =0>

Text =Menu2

Margin =10 0 0 0

Horizo​​ntalAlignment =Left

VerticalAlignm ent =中心

FontSize =12

前景=黑色

/>





< grid x:name =grdMenu3removed =Transparentgrid.row =2tapped =Menu3_Tapped >

< stackpanel margin =5orientation =水平>

<图片x:名称=imgMenu3高度=20宽度=20Source =ms-appx:///Assets/Menu3.pngStretch =UniformToFill/>

< textblock x:name =Menu3grid.row = 0text =Menu3>

Margin =10 0 0 0

Horizo​​ntalAlignment =Left

VerticalAlignment =中心

FontSize =12

前景=黑色/>





< grid x:name =grdMenu4removed =Transparentgrid.row =3tapped =Menu4_Tapped>

< stackpanel margin =5orientation =Horizo​​ntal>

< Image x:Name =imgMenu4Height =20Width =20Source =ms-appx:///Assets/Menu4.pngStretch =UniformToFill/> ;

< textblock x:name =Menu4grid.row =0text =Menu4>

Margin =10 0 0 0

Horizo​​ntalAlignment =左

VerticalAlignment =中心

FontSize =12

前景=黑色/>





< grid x:name =grdMenu5removed =Transparentgrid.row =4 tapped =Menu5_Tapped>

< stackpanel margin =5orientation =Horizo​​ntal>

< Image x:Name =imgMenu5Height =20Width =20Source =ms-appx: ///Assets/Menu5.pngStretch =UniformToFill/>

< textblock x:name =Menu5grid.row =0text =Menu5>

Margin =10 0 0 0

Horizo​​ntalAlignment =Left

VerticalAlignment =Center

FontSize =12

前景=黑色/>





< grid x:name =grdMenu6removed =Transparentgrid.row =5tapped =Menu6_Tapped>

< stackpanel margin =5orientation =Horizo​​ntal>

< Image x:姓名=imgMenu6Height =20Width =20Source =ms-appx:///Assets/Menu6.pngStretch =UniformToFill/>

< textblock x :name =Menu6grid.row =0text =Menu6>

Margin =10 0 0 0

Horizo​​ntalAlignment =Left

VerticalAlignment =中心

FontSize =12

前景=黑色/>





< grid x:name =grdMenu7removed =Transparentgrid.row =6tapped =Menu7_Tapped>

< stackpanel margin =5orientation =Horizo​​ntal>

< Image x:Name =imgMenu7Height =20Width =20

Source =ms-appx:/// Assets / Menu7。 pngStretch =UniformToFill/>

< textblock x:name =Menu7grid.row =0>

Text =Menu7

保证金=10 0 0 0

Horizo​​ntalAlignment =左

VerticalAlignment =中心

FontSize =12

Foreground =Black/>



















源代码如下所示 -



使用系统;

使用System.Collections.Generic;

使用System.IO;

使用System.Linq;

使用System.Runtime.InteropServices.WindowsRuntime;

使用Windows.Foundation;

使用Windows.Foundation.Collections;

使用Windows.UI.Core;

使用Windows.UI.Xaml;

使用Windows.UI.Xaml.Controls;

使用Windows.UI.Xaml.Controls.Primitives;

使用Windows.UI.Xaml.Data;

使用Windows.UI.Xaml.Input;

使用Windows.UI.Xaml.Media;

使用Windows.UI.Xaml.Navigation;



//用户控件项模板记录在http://go.microsoft.com/fwlink/?LinkId = 234236



名称空间Sample.Controls

{

公共密封部分类DrawerNavigation:UserControl

{

public DrawerNavigation()

{

this.InitializeComponent();

Loaded + = Navigation_Loaded;

}



async void Navigation_Loaded(object sender,RoutedEventArgs e)

{

等待Windows.UI.ViewManagement.StatusBar.GetForCurrentView()。HideAsync();

DrawerLayout.InitializeDrawerLayout();

Windows.Phone.UI.Input.HardwareButtons。 BackPressed + = HardwareButtons_BackPressed;

}



void HardwareButtons_BackPressed(object sender,Windows.Phone.UI.Input.BackPressedEventArgs e)

{

if(DrawerLayout.IsDrawerOpen)

{

DrawerLayout.CloseDrawer();

e.Handled = true;

}

else

{

Application.Current.Exit();

}

}



private async void Menu1_Tapped(object sender,TappedRoutedEventArgs e)

{

等待Dispatcher.RunAsync(CoreDispatcherPriority.Normal,()=> (Window.Current.Content as Frame).Navigate(typeof(Menu1)));

}



private async void Menu2_Tapped(object sender,TappedRoutedEventArgs e)

{

等待Dispatcher.RunAsync(CoreDispatcherPriority.Normal,()=>(Window.Current.Content as Frame).Navigate(typeof( Menu2)));

}



private async void Menu3_Tapped(object sender,RoutedEventArgs e)

{

等待Dispatcher.RunAsync(CoreDispatcherPriority.Normal,()=>(Window.Current.Content as Frame).Navigate(typeof(Menu3)));

}



private async void Menu4_Tapped(object sender,RoutedEventArgs e)

{

await Dispatcher.RunAsync(CoreDispatcherPriority) .Normal,()=>(Window.Current.Content as Frame).Navigate(typeof(Menu4)));

}



私人异步voi d Menu5_Tapped(对象发送者,RoutedEventArgs e)

{

await Dispatcher.RunAsync(CoreDispatcherPriority.Normal,()=> (Window.Current.Content as Frame).Navigate(typeof(Menu5)));

}



private async void Menu6_Tapped(object sender,RoutedEventArgs e)

{

等待Dispatcher.RunAsync(CoreDispatcherPriority.Normal,()=>(Window.Current.Content as Frame).Navigate(typeof( Menu6)));

}



private void Menu7_Tapped(object sender,RoutedEventArgs e)

{

Application.Current.Exit();

}



private void DrawerIcon_Tapped(object sender,TappedRoutedEventArgs e)

{

if(DrawerLayout.IsDrawerOpen)

{

DrawerLayout.CloseDrawer();

}

其他

{

DrawerLayout.OpenDrawer();

}

}

}

}



我的问题是,当我第一次从一个页面导航到另一个页面时,它正确导航,但下次如果我再次选择相同的页面,它从DrawerLayout.dll抛出以下错误



对于ex。假设将Menu1导航到Menu2。

再次尝试从Menu2移动到Menu1,它将生成以下异常。



mscorlib.ni.dll中出现System.ArgumentException类型的异常,但未在用户代码中处理



WinRT信息:名称已存在于树中:_shadowFragment。



附加信息:参数不正确。



树中已经存在名称:_shadowFragment。



如果有这个例外的处理程序,程序可能是安全地继续。









还有一件事如果页面导航两个页面应该有网格&堆栈面板控制。



提前致谢。

Rohan

I am creating windows phone app in VS 2013 with version WP 8.1.
I am creating Menu Navigation control using drawer layout control for Windows Phone 8.1 from NuGet,

1. Right Click on Add Reference -> Manage NuGet Packages -> Add "DrawerLayout" text in Search box.
2. Select "DrawerLayout For Windows Phone 8.1" and click on Install button.

I have created user control to use it commonly from all the pages to navigating.
My XAML Design for User is given below -

<usercontrol>
x:Class="Sample.Controls.DrawerNavigation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dwLayout="using:DrawerLayout"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<grid x:name="RootLayout" xmlns:x="#unknown">
<grid.rowdefinitions>
<rowdefinition height="Auto">
<rowdefinition height="*">

<!-- Title Bar -->
<grid x:name="TitleBar" removed="Orange" grid.row="0" height="30">
<grid.columndefinitions>
<columndefinition width="Auto"> <columndefinition width="*">

<Image x:Name="DrawerIcon"
Margin="5,5,0,5"
Grid.Column="0"
Source="/Assets/drawer_icon_1.png"
HorizontalAlignment="Left"
Tapped="DrawerIcon_Tapped"
Grid.ColumnSpan="2" />
<textblock x:name="txtHeader">
Grid.Column="2"
Foreground="White"
VerticalAlignment="Center"
FontSize="15"
Margin="35,0,0,-5"
Height="23"/>

<!-- Drawer Layout -->
<dwlayout:drawerlayout xmlns:dwlayout="#unknown">
Grid.Row="1"
x:Name="DrawerLayout"
removed="Transparent">
<!--MainPage -->
<grid x:name="MainFragment" removed="Transparent">

<grid x:name="ListFragment" removed="White">
<scrollviewer>
<grid>
<grid.rowdefinitions>
<rowdefinition height="Auto">
<rowdefinition height="40">
<rowdefinition height="40">
<rowdefinition height="40">
<rowdefinition height="40">
<rowdefinition height="40">
<rowdefinition height="40">

<grid x:name="grdMenu1" removed="Transparent">
Grid.Row="0" Tapped="Menu1_Tapped">
<stackpanel margin="5">
Orientation="Horizontal">
<Image x:Name="imgMenu1"
Height="20" Width="20"
Source="ms-appx:///Assets/Menu1.png"
Stretch="UniformToFill" />
<textblock x:name="Menu1">
Grid.Row="0"
Text="Menu1"
Margin="10 0 0 0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
FontSize="12"
Foreground="Black"/>


<grid x:name="grdMenu2">
removed="Transparent" Grid.Row="1" Tapped="Menu2_Tapped" >
<stackpanel margin="5" orientation="Horizontal">
<Image x:Name="imgMenu2" Height="20" Width="20" Source="ms-appx:///Assets/Menu2.png" Stretch="UniformToFill" />
<textblock x:name="Menu2" grid.row="0">
Text="Menu2"
Margin="10 0 0 0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
FontSize="12"
Foreground="Black"
/>


<grid x:name="grdMenu3" removed="Transparent" grid.row="2" tapped="Menu3_Tapped">
<stackpanel margin="5" orientation="Horizontal">
<Image x:Name="imgMenu3" Height="20" Width="20" Source="ms-appx:///Assets/Menu3.png" Stretch="UniformToFill" />
<textblock x:name="Menu3" grid.row="0" text="Menu3">
Margin="10 0 0 0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
FontSize="12"
Foreground="Black"/>


<grid x:name="grdMenu4" removed="Transparent" grid.row="3" tapped="Menu4_Tapped">
<stackpanel margin="5" orientation="Horizontal">
<Image x:Name="imgMenu4" Height="20" Width="20" Source="ms-appx:///Assets/Menu4.png" Stretch="UniformToFill" />
<textblock x:name="Menu4" grid.row="0" text="Menu4">
Margin="10 0 0 0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
FontSize="12"
Foreground="Black" />


<grid x:name="grdMenu5" removed="Transparent" grid.row="4" tapped="Menu5_Tapped">
<stackpanel margin="5" orientation="Horizontal">
<Image x:Name="imgMenu5" Height="20" Width="20" Source="ms-appx:///Assets/Menu5.png" Stretch="UniformToFill" />
<textblock x:name="Menu5" grid.row="0" text="Menu5">
Margin="10 0 0 0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
FontSize="12"
Foreground="Black"/>


<grid x:name="grdMenu6" removed="Transparent" grid.row="5" tapped="Menu6_Tapped">
<stackpanel margin="5" orientation="Horizontal">
<Image x:Name="imgMenu6" Height="20" Width="20" Source="ms-appx:///Assets/Menu6.png" Stretch="UniformToFill" />
<textblock x:name="Menu6" grid.row="0" text="Menu6">
Margin="10 0 0 0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
FontSize="12"
Foreground="Black"/>


<grid x:name="grdMenu7" removed="Transparent" grid.row="6" tapped="Menu7_Tapped">
<stackpanel margin="5" orientation="Horizontal">
<Image x:Name="imgMenu7" Height="20" Width="20"
Source="ms-appx:///Assets/Menu7.png" Stretch="UniformToFill" />
<textblock x:name="Menu7" grid.row="0">
Text="Menu7"
Margin="10 0 0 0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
FontSize="12"
Foreground="Black"/>









It Source Code Is as given Below -

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace Sample.Controls
{
public sealed partial class DrawerNavigation : UserControl
{
public DrawerNavigation()
{
this.InitializeComponent();
Loaded += Navigation_Loaded;
}

async void Navigation_Loaded(object sender, RoutedEventArgs e)
{
await Windows.UI.ViewManagement.StatusBar.GetForCurrentView().HideAsync();
DrawerLayout.InitializeDrawerLayout();
Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}

void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
if (DrawerLayout.IsDrawerOpen)
{
DrawerLayout.CloseDrawer();
e.Handled = true;
}
else
{
Application.Current.Exit();
}
}

private async void Menu1_Tapped(object sender, TappedRoutedEventArgs e)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => (Window.Current.Content as Frame).Navigate(typeof(Menu1)));
}

private async void Menu2_Tapped(object sender, TappedRoutedEventArgs e)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => (Window.Current.Content as Frame).Navigate(typeof(Menu2)));
}

private async void Menu3_Tapped(object sender, RoutedEventArgs e)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => (Window.Current.Content as Frame).Navigate(typeof(Menu3)));
}

private async void Menu4_Tapped(object sender, RoutedEventArgs e)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => (Window.Current.Content as Frame).Navigate(typeof(Menu4)));
}

private async void Menu5_Tapped(object sender, RoutedEventArgs e)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => (Window.Current.Content as Frame).Navigate(typeof(Menu5)));
}

private async void Menu6_Tapped(object sender, RoutedEventArgs e)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => (Window.Current.Content as Frame).Navigate(typeof(Menu6)));
}

private void Menu7_Tapped(object sender, RoutedEventArgs e)
{
Application.Current.Exit();
}

private void DrawerIcon_Tapped(object sender, TappedRoutedEventArgs e)
{
if (DrawerLayout.IsDrawerOpen)
{
DrawerLayout.CloseDrawer();
}
else
{
DrawerLayout.OpenDrawer();
}
}
}
}

My Question is that when i am navigating from one page to another page first time it navigating properly, but next time If i select same page again it throwing below error from DrawerLayout.dll

For ex. Suppose Navigating Menu1 to Menu2.
again try to Move from Menu2 to Menu1 it will generate below exception.

An exception of type 'System.ArgumentException' occurred in mscorlib.ni.dll but was not handled in user code

WinRT information: The name already exists in the tree: _shadowFragment.

Additional information: The parameter is incorrect.

The name already exists in the tree: _shadowFragment.

If there is a handler for this exception, the program may be safely continued.




One more thing is that if while page navigating both the pages should have grid & stack panel control.

Thanks in advance.
Rohan

推荐答案

大家好,



我得到了这个问题的解决方案。



但另一个问题是,当我在父页面上使用DrawerNavigation Control时,它不会在父页面上生成事件。



例如 -



在Menu1.xaml上添加了用户控件的引用,如下所示 -

xmlns:uc = using:Sample.Controls



并在Menu1.xaml上使用用户控件,如下所示 -



< uc:drawernavigation x:name =ucDNMenu1zindex =999xmlns:x =#unknownxmlns:uc =#unknown>< / uc:drawernavigation>



在每个页面上添加用户控件。



提前致谢,

Rohan
Hi all,

I got the solution for this issue.

But facing another issue is that When i using DrawerNavigation Control on the parent page it not generating events on the parent page.

For Example -

On Menu1.xaml added reference of user control like given below -
xmlns:uc ="using:Sample.Controls"

And using user control on Menu1.xaml as given below-

<uc:drawernavigation x:name="ucDNMenu1" zindex="999" xmlns:x="#unknown" xmlns:uc="#unknown"></uc:drawernavigation>

Added user control on every pages.

Thanks in advance,
Rohan


这篇关于该名称已存在于树中,在Windows Phone 8.1应用程序开发中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆