简体   繁体   中英

What is causing “The type or namespace name 'UI' does not exist in the namespace” error?

I am working on a UWP application and I am trying to do something with the values of a SelectedItem in a GridView. So as I usually do with WPF I went into the XAML and Typed my event name in the XAML and let VS create the event in code for me.

<GridView x:Name="DataGrid1"
        ItemClick="dg_ItemClick">
        <!-- Just -->
        <!-- Some -->
        <!-- Normal -->
        <!-- XAML -->
</GridView>

and the VS created event looks like this

    private void dg_ItemClick(object sender, Windows.UI.Xaml.Controls.ItemClickEventArgs e)
    {
            Biz.Customer cust = e.ClickedItem as Biz.Customer;
            var messageDialog2 = new MessageDialog(cust.Code, cust.Company);
            messageDialog2.ShowAsync();
    }

Right away I get a red squiggly line under the UI in the Windows.UI.Xaml.Controls part. There is a using at the top of the page to include that and VS wouldn't let add any other UWP references.

I can get the error to go away as I have in other parts of the application by changing the ItemClickEventArgs to RoutedEvenArgs but in this case I really need the ClickedItem as an argument to be passed to the code behind.

I have just talked to someone else getting started with UWP and they said they are having the same issue on other cLick events. So are we both doing something wrong or is there something missing from our application that is needed for the UI to be recognized?

The full error I am getting is this: Error CS0234 The type or namespace name 'UI' does not exist in the namespace 'app name' (are you missing an assembly reference?)

The type or namespace name 'UI' does not exist in the namespace 'app name'

That means you have a namespace in your app that contains Windows .

Either change your namespace, or prefix the UWP one:

global::Windows.UI.Xaml.Controls

See also How to: Use the Global Namespace Alias (C# Programming Guide) .

One of bugs I can see is that your handler should be marked async , also you should use await operator when you call async method:

private async void dg_ItemClick(object sender, Windows.UI.Xaml.Controls.ItemClickEventArgs e)
{
        Biz.Customer cust = e.ClickedItem as Biz.Customer;
        var messageDialog2 = new MessageDialog(cust.Code, cust.Company);
        await messageDialog2.ShowAsync();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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