User avatar
Regbalt
Topic Author
Posts: 11
Joined: 21 Feb 2018, 16:13

Error: does not contain a definition for ....

24 Feb 2018, 19:03

I'am learning at the moment wpf and try to test things in Unity3D.

But at the Moment I hit a wall.

Unity keeps yelling at me:
Assets/MyResources/UI/UserControl1.xaml.cs(26,18): error CS1061: Type `UI.UserControl1' does not contain a definition for `List1' and no extension method `List1' of type `UI.UserControl1' could be found. Are you missing an assembly reference?
In Visual Studio, it finds List1 without problems, but Noesis Unity don't.
this.List1.Items.Add("Test");
#if UNITY_5_3_OR_NEWER
#define NOESIS
using Noesis;
#else
using System;
using System.Windows;
using System.Windows.Controls;
#endif
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace UI
{
    /// <summary>
    /// Interaktionslogik für UserControl1.xaml
    /// </summary>
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            collection = new ItemCollection();
            this.DataContext = collection;

            this.Initialized += UserControl1_Initialized;
            this.InitializeComponent();

        }

        ItemCollection collection;

        public class ItemCollection : NotifyPropertyChangedBase
        {
            ObservableCollection<string> items;

            public ObservableCollection<string> Items
            {
                get
                {
                    if(items == null) items = new ObservableCollection<string> { "One", "Two", "Three" };
                    return items;
                }
            }

            public void addItem(string text)
            {
                items.Add(text);
                OnPropertyChanged("Items");
            }
        }

        public void UserControl1_Initialized(object sender, EventArgs e)
        {

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            collection.addItem("TestBBB");
        }

#if NOESIS
        void InitializeComponent()
        {
            Noesis.GUI.LoadComponent(this, "Assets/MyResources/UI/UserControl1.xaml");
        }
#endif
    }
}
<UserControl x:Class="UI.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:UI"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             Background="#FF2B6EA8"
             
             >
    <Border Padding="10">
        <StackPanel>
            <Button Content="Test" Click="Button_Click" />

            <ListView x:Name="List1" Background="Transparent" Height="200" ItemsSource="{Binding Items}">
                <ListView.Resources>
                    <Style TargetType="ListViewItem">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                        <Setter Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <TextBlock Text="{Binding}" Margin="0 0 0 2" Background="Wheat"/>
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </ListView.Resources>
            </ListView>
            
        </StackPanel>
    </Border>
</UserControl>
I really get a headache. Where does compile when, was ?
I try to compare with the samples, but that don't help.

Sorry then this is a noob question.

UPDATE:
Tried it with this complicated mvvm, but the List never updates.
And after some time Unity yells at me, that Button_Click is not defined, but this is defined in the class and it has worked 1min before.
What a buggy mess. Sorry, get really frustrated right now.
 
User avatar
sfernandez
Site Admin
Posts: 1918
Joined: 22 Dec 2011, 19:20

Re: Error: does not contain a definition for ....

28 Feb 2018, 20:35

Have you read and followed UserControl tutorial for Unity?
In Visual Studio, it finds List1 without problems, but Noesis Unity don't.
WPF generates hidden code with variables for each named element in the XAML. That code is not available to Unity, so if you want to share the same code you need to define these variables inside a code block for Unity, for example:
#if NOESIS
        private ListView List1;

        void InitializeComponent()
        {
            Noesis.GUI.LoadComponent(this, "Assets/MyResources/UI/UserControl1.xaml");
            
            List1 = (ListView)FindName("List1");
        }
#endif
And after some time Unity yells at me, that Button_Click is not defined, but this is defined in the class and it has worked 1min before.
In the tutorial I mentioned before you can see how to connect xaml events to code-behind class functions (via overriding ConnectEvent method):
    protected override bool ConnectEvent(object source, string eventName, string handlerName)
    {
        if (eventName == "Click" && handlerName == "Button_Click")
        {
            ((Button)source).Click += Button_Click;
            return true;
        }
        return false;
    }
Tried it with this complicated mvvm, but the List never updates.
For MVVM please read Data Binding tutorial, and you can see that most of our samples in Unity package follow the MVVM pattern to connect data with UI elements.

Who is online

Users browsing this forum: No registered users and 4 guests