简体   繁体   English

从 Page1.xaml 导航到 Page2.xaml。 UWP xaml。 没有为此对象定义无参数构造函数。 Frame.Navigate 带参数

[英]Navigating from Page1.xaml to Page2.xaml. UWP xaml. No parameterless constructor defined for this object. Frame.Navigate with parameters

Making a UWP app.制作 UWP 应用。 Trying to navigate from Page1.xaml to Page2.xaml passing some parameters to Page2.xaml and I get the following error: No parameterless constructor defined for this object.尝试从 Page1.xaml 导航到 Page2.xaml,将一些参数传递给 Page2.xaml,我收到以下错误:没有为此对象定义无参数构造函数。

Page1.xaml has a for loop that adds buttons to navigate to Page2.xaml. Page1.xaml 有一个 for 循环,用于添加导航到 Page2.xaml 的按钮。 Page2.xaml content is created dynamically and has different content depending on when button is clicked. Page2.xaml 内容是动态创建的,并且根据单击按钮的时间具有不同的内容。

Example of buttons created: App 1 App 2 Game 1 Game 2创建的按钮示例: App 1 App 2 Game 1 Game 2

When you click on each button, different content is produced on Page2.xaml.当您单击每个按钮时,会在 Page2.xaml 上生成不同的内容。

Code to add buttons on Page1.xaml with in the for loops is:在 for 循环中在 Page1.xaml 上添加按钮的代码是:

Button OpenAppButton = new Button();
OpenAppButton.Click += async delegate { Frame.Navigate(typeof(AppDisplay), AppDataTemp); };

AppDisplay is Page2.xaml. AppDisplay 是 Page2.xaml。 AppDataTemp is the parameters [string] being passed to Page2.xaml. AppDataTemp 是传递给 Page2.xaml 的参数 [string]。

AppsDisplayStack.Children.Add(OpenAppButton);

The button is added to a StackPanel called AppDisplayStack.该按钮被添加到名为 AppDisplayStack 的 StackPanel。

Page2.xaml is the following code: Page2.xaml 是以下代码:

    <Grid>
        <StackPanel>
            <ScrollViewer>
                <StackPanel x:Name="AppStack" VerticalAlignment="Top" HorizontalAlignment="Center">
                </StackPanel>
            </ScrollViewer>
        </StackPanel>
    </Grid>

Everything else in Page2.xaml is created dynamically. Page2.xaml 中的其他所有内容都是动态创建的。

I have a feeling that I'm not passing the parameter correctly.我有一种感觉,我没有正确传递参数。 If I try without parameters, Page2.xaml loads.如果我尝试不带参数,Page2.xaml 会加载。 Any ideas?有任何想法吗? Thanks in advance for any help.在此先感谢您的帮助。

Edit: Full error code: {System.MissingMethodException: No parameterless constructor defined for this object.编辑:完整错误代码:{System.MissingMethodException:没有为此对象定义无参数构造函数。 at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean wrapExceptions, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean wrapExceptions, Boolean skipCheckThis, Boolean fillCache) at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, Boolean wrapExceptions) at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions) at Microsoft.UI.Xaml.Markup.XamlReflectionType.ActivateInstance() at Windows.UI.Xaml.Controls.Frame.Navigate(Type sourcePageType, Object parameter) at PermissionsAuditorViewer.AppsNGames.<>c__DisplayClass15_0.<b__0>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.b__7_0(Object state) at System.Threading.WinRTSynchronizationContextBase.Invoker.InvokeCore()}在 System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean wrapExceptions, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean wrapExceptions, Boolean skipCheckThis, Boolean fillCache) 在 System.RuntimeType.CreateInstanceOnlyCtor(Boolean publicBoolean public , Boolean skipCheckThis, Boolean fillCache, Boolean wrapExceptions) at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions) at Microsoft.UI.Xaml.Markup.XamlReflectionType.ActivateInstance() at Windows.UI.Xaml.Controls.Frame .Navigate(Type sourcePageType, Object parameter) at PermissionsAuditorViewer.AppsNGames.<>c__DisplayClass15_0.<b__0>d.MoveNext() --- 在 System.Runtime.CompilerServices 处抛出异常的前一个位置的堆栈跟踪结束。 AsyncMethodBuilderCore.<>c.b__7_0(Object state) at System.Threading.WinRTSynchronizationContextBase.Invoker.InvokeCore()}

Don't know if that helps.不知道这是否有帮助。

UDPATE: Found the problem. UDPATE:发现问题。 I forgot that I changed the constructor for Page2.xaml from "public AppDisplay()" to "public AppDisplay(string URLData)".我忘了我将 Page2.xaml 的构造函数从“public AppDisplay()”更改为“public AppDisplay(string URLData)”。 This was causing the issue.这导致了这个问题。 Thanks for the help guys.谢谢你们的帮助。 Appreicate it.欣赏它。

Would you be able to post more code or explain more?你能发布更多代码或解释更多吗? What is AppDataTemp and its type and are you pulling in e.Parameter or e.Parameter.YourModelsProperty on the other side of passing?什么是 AppDataTemp 及其类型,您是否在e.Parameter的另一侧e.Parametere.Parameter.YourModelsProperty If you are pulling from one page to another and binding its value to another instance of that object it will need to be the same type on both page's code-behind.如果您从一个页面拉到另一个页面并将其值绑定到该对象的另一个实例,则两个页面的代码隐藏中的类型都需要相同。

If your issue is No parameterless constructor defined for this object then it looks like you are not defining the frame correctly whether it be page 1 or page to (to or from), or you need to declare a variable of the same type to bind to whatever data you are passing with your navigation.如果您的问题是No parameterless constructor defined for this object那么您似乎没有正确定义框架,无论是第 1 页还是第 1 页(到或从),或者您需要声明要绑定到的相同类型的变量您通过导航传递的任何数据。 From your explanation it sounds like you need to declare a model or your frame and will need something like从你的解释看来,你需要声明一个模型或你的框架,并且需要类似的东西

Frame rootFrame = this.Frame();

or或者

YourModel VarName = new YourModel(); on both pages or create an object on the page YourModel Varname { get; set; }在两个页面上或在页面上创建一个对象YourModel Varname { get; set; } YourModel Varname { get; set; } YourModel Varname { get; set; } on both pages and define it in your navigation function with VarName = e.Parameter; YourModel Varname { get; set; }两个页面上,并与您的导航功能将其定义VarName = e.Parameter;

or if you are passing in a simple bool, string, int, etc.或者如果您传入一个简单的 bool、string、int 等。

string VarName { get; set; } string VarName { get; set; } on both pages, and you can bind that to either VarName = e.Parameter or VarName = e.Parameter.YourModelStringProp string VarName { get; set; }两个页面上,你可以绑定是要么VarName = e.ParameterVarName = e.Parameter.YourModelStringProp

UPDATE: An example of using e.Parameter in your case would be something in your OnNavigated functions like:更新:在您的情况下使用 e.Parameter 的示例是您的 OnNavigated 函数中的某些内容,例如:

YourModel VarName { get; set; }
string YourStr { get; set; }
protected override void OnNavigatedTo(NavigationEventArgs e){
    base.OnNavigatedTo(e);
    if (e.Parameter is string){  //common variable
        YourStr = e.Parameter;  //YourStr = e.Parameter.StrProp; //may need .ToString() for some
    }
    else //using a Model
    {
      YourModel = e.Parameter; // YourModel.YourProp = e.Parameter.YourProp;
    }
    //Use newly bound YourModel/YourStr here/elsewhere or call another function
    //function();
}

UPDATE 2: Would you be able to add more code with that error I believe it has something to do with your Navigation and declaring the Frame or declaring which Page you are Navigating too, if you post some code I may be able to see it easier.更新 2:您能否添加更多带有该错误的代码我相信它与您的导航和声明框架或声明您正在导航的页面有关,如果您发布一些代码,我可能会更容易看到它. Im sure youre really close but I believe you forgot to declare your frame in OpenAppButton.Click += async delegate { Frame.Navigate(typeof(AppDisplay), AppDataTemp); };我确定你真的很接近,但我相信你忘了在OpenAppButton.Click += async delegate { Frame.Navigate(typeof(AppDisplay), AppDataTemp); };声明你的框架OpenAppButton.Click += async delegate { Frame.Navigate(typeof(AppDisplay), AppDataTemp); }; OpenAppButton.Click += async delegate { Frame.Navigate(typeof(AppDisplay), AppDataTemp); }; and Im not sure how your layout is but you should be able to pull whatever you declared your Frame from in App.xaml.cs or create an instance on each page using something like而且我不确定您的布局如何,但是您应该能够从 App.xaml.cs 中提取您声明的 Frame 的任何内容,或者使用类似的方法在每个页面上创建一个实例

Frame rootFrame = Window.Current.Content as Frame;

then change that line to然后将该行更改为

OpenAppButton.Click += async delegate { rootFrame.Navigate(typeof(AppDisplay), AppDataTemp); };

If this doesnt help post some more code and I should be able to solve whats causing the issue.如果这对发布更多代码没有帮助,我应该能够解决导致问题的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM