Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Xamarin.Forms app not apply code changes from second project assembly without clean/rebuild solution

Hi all.
I use the VS Professional 2019 Version 16.7.7 and latest Xamarin.Forms. Work only with Android part on Windows 10. I have the simple blank app with one MainPage and one MainViewModel, where the viewmodels part implemented as separate assembly. In the ctor of viewmodel I initializes test string, which show on UI via Binding. And the issue in the next: when I start application first - everything is okey, but if I change some code in the ViewModels assembly (for example - change test string value) and relaunch - application not apply these changes without clean/rebuild solution. And even breakpoint show the old property value.


I leave the GD link for my solution, if someone want to try to reproduce issue https://drive.google.com/file/d/1507isz5lX_U6204y5o0_maq7ehBeHKFq

Waiting for helps. Thanks.

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    From your code ,I found that you want to update the binded text of a Label after you run the app .

    And I noticed that you just modified the text in the construct of the MainPageViewModel. Since we all know that the constructor of a class is called only once when a class is created.

    So if you change the text in the constructor, it doesn't work.

    You can add a Button in this page, and modify the text when you click the Button.

    For example,we can modify your code like this:

    1.add a button in MainPage.xaml file :

        <StackLayout VerticalOptions="Center">
            <Label Text="{Binding TestString}" FontSize="20" TextColor="Red" Padding="30,0,30,0"/>
    
            <Button Text="update" Clicked="Button_Clicked" />
        </StackLayout>
    

    2.add a function in class MainPageViewMode:

        public void updateStringValue(string str) {
            TestString = str;
        }
    

    The whole code of MainPageViewMode:

        public class MainPageViewModel : BaseViewModel
        {
            private string _testString;
    
            public MainPageViewModel()
            {
                TestString = "Some  VALUE zaaaaaaaaa ";
            }
    
            public void updateStringValue(string str) {
                TestString = str;
            }
    
            public string TestString
            {
                get => _testString;
                set => SetProperty(ref _testString, value);
            }
        }
    
    1. MainPage.xaml.cs
        public partial class MainPage : ContentPage
        {
            public MainPage()
            {
                InitializeComponent();
            }
    
            private void Button_Clicked(object sender, EventArgs e)
            {
                MainPageViewModel model = BindingContext as MainPageViewModel;
    
                model.updateStringValue("New Text without Rebuild");
    
            }
        }
    
  • DanilKachanovDanilKachanov UAMember ✭✭

    @jezh Thank you, but I change text in different launch session (not in one launch session). So, I launch app, stop it, change text in constructor and launch again. In this case new constructor should be apply, but not happens.

  • jezhjezh Member, Xamarin Team Xamurai

    but I change text in different launch session (not in one launch session). So, I launch app, stop it, change text in constructor and launch again.

    What do you mean by words in different launch session, I just found you modified the text in the constructor of MainPageViewModel

        public MainPageViewModel()
        {
            TestString = "NEW TEXT WITHOUT REBUILD";
        }
    
  • DanilKachanovDanilKachanov UAMember ✭✭

    @jezh You absolutely right that I want to change text in constructor, but you are not right in:

    @jezh said:
    From your code ,I found that you want to update the binded text of a Label after you run the app .

    A did not change text after run the app. I stopped app, changed text and launch again with new changes, but changes not apply. You are not fully correct understanding my problem.

  • jezhjezh Member, Xamarin Team Xamurai
    edited November 2020

    A did not change text after run the app. I stopped app, changed text and launch again with new changes, but changes not apply

    Yes, it is just the case as you said.

    After we changed the text, and if we didn't clean/rebuild the app, the modified code will not apply to our app.

    Because the app still used the previous compiled files.

    So what do these features (clean, Build and Rebuild )do?

    A simple explanations:

    • Clean Solution - deletes all compiled files (all dll’s and exe’s ).
    • Build Solution - compiles code files (dll and exe) that have changed.
    • Rebuild Solution - Deletes all compiled files and Compiles them again regardless of whether or not the code has changed.

    For more details about this, you can check: https://bitwizards.com/thought-leadership/blog/2014/august-2014/visual-studio-why-clean-build-rebuild

  • shaunpryszlakshaunpryszlak USMember ✭✭✭

    @DanilKachanov I think there is something wrong with your solution. I downloaded it and ran it and got the results you reported. For some reason it is not recompiling the ViewModels project, so if you put a breakpoint in the constructor, TestString only ever gets assigned the value from the first compilation.

    In the menu where it says "Debug", "AnyCPU", "TempApp.Android" click the debug dropdown and click "configuration Manager". All the projects are ticked so they should all be rebuilt if anything changes.

    Have you been changing the settings or anything like that?

  • DanilKachanovDanilKachanov UAMember ✭✭
    edited November 2020

    @shaunpryszlak Yes I also investigated such settings as config manager and some other to find the problem, but no result. And about your answer - no, I didn't apply some changes in project or solution settings.
    I faced this issue when started the real production project and made it architecture. Then I created the simple new project with only two assemblies for reproduce issue anyone who want by GD link.

    But, in general i am glad someone already can reproduce it and understand that something here is incorrect (maybe VS or Xam Forms). Did you try to create similar project with two assemblies on your environment?

  • Tracy320Tracy320 Member ✭✭✭

    Xamarin.Forms app not apply code changes from second project assembly without clean/rebuild solution

    In general, when we modify our code, we usually clean or rebuild it.
    I don't understand why don't you clean or rebuild your app?

Sign In or Register to comment.