dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.26k stars 1.76k forks source link

.NET MAUI Binding issue CS0103: There is no context exist here | .NET 9 Core #25891

Open SoumyadipYT-OSS opened 6 days ago

SoumyadipYT-OSS commented 6 days ago

Description

I am unable to bind the value from API to xaml UI rendering.

My xamlpage looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="WeatherApp.WeatherPage"
             Title="WeatherPage">

    <Grid RowDefinitions="Auto,Auto,Auto,Auto,Auto"
          RowSpacing="20">
        <StackLayout Grid.Row="0" 
                     Margin="20,20,20,0"
                     Orientation="Horizontal">

            <!-- In .NET 9 Frame is obsolete so I used Border here -->
            <Border Padding="10" 
                    Stroke="LightGray"
                    StrokeThickness="1"
                    x:Name="AnimatedBorder">
                <Border.StrokeShape>
                    <RoundRectangle CornerRadius="35" />
                </Border.StrokeShape>
                <Border.Shadow>
                    <Shadow Brush="MediumSlateBlue" Opacity="0.5" Offset="5,5" />
                </Border.Shadow>
                <Label Text="Your Location"
                       FontSize="Default" />
            </Border>
        </StackLayout>

        <StackLayout Grid.Row="1"
                     Orientation="Vertical">
            <Label x:Name="LblCity"
                   FontSize="40" HorizontalOptions="Center"
                   TextColor="PeachPuff" />

            <Label x:Name="LblWeatherDescription"
                   FontSize="Medium"
                   HorizontalOptions="Center"
                   TextColor="AntiqueWhite" />
        </StackLayout>

Now, see carefully the LblCity and LblDescription part where I am using the x:Name to display the value from the .cs file.

Now xaml.cs file looks like this:

namespace WeatherApp {
    public partial class WeatherPage : ContentPage {
        public WeatherPage() {
            InitializeComponent();
        }

        protected async override void OnAppearing() {
            base.OnAppearing();
            var result = await ApiService.GetWeather(47.6829, -122.1209);
            Dispatcher.Dispatch(() => {

            });
            if (result != null && result.city != null && result.list != null && result.list.Count > 0) {
                Dispatcher.Dispatch(() => {
                    LblCity.Text = result.city.name;
                    LblWeatherDescription.Text = result.list[0].weather[0].description;
                });
            }
        }
    }
}

But it is showing error, CS0103: LblCity context does not exist.

In .NET 9 it is not working, like Frame is removed in .NET 9, so if there is new way to implement the rendering please tell. Issue is open.

Steps to Reproduce

No response

Link to public reproduction project repository

No response

Version with bug

9.0.10 SR1

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

8.0.0-rc.1.9171

Affected platforms

Android, Windows, macOS, iOS, Other (Tizen, Linux, etc. not supported by Microsoft directly)

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

jfversluis commented 5 days ago

Can you maybe show the full code and/or add a reproduction? There can be reasons that this is not working that are not shown in the XAML you have posted now. Like is this part of a DataTemplate maybe? Because then you cannot reference controls like this.

But you say this exact code worked in .NET 8? Is it this exact code or did you change anything besides the Border when you moved to .NET 9? Did you remove the bin & obj folders from your project and do a rebuild? Are there any other errors when you build that might cause this to happen?

SoumyadipYT-OSS commented 5 days ago

@jfversluis Sir I have uploaded the xaml code there and in the xaml.cs file there are many errors coming related to CS:0103

SoumyadipYT-OSS commented 5 days ago

@jfversluis Sir I have added my GitHub repository link here where you can find the code related to this topic:

https://github.com/SoumyadipYT-OSS/MAUI-RealTimeWeather-Application/blob/main/WeatherApp%2FWeatherPage.xaml.cs