blazor calling a componet with parameters code example

Example 1: how to pass property between blazor components

@page "/page2"
@inject Services.AppData AppData

<h1>Second Page</h1>
<p>You entered the number @AppData.Age.</p>

Example 2: how to pass property between blazor components

using System;
namespace AppDataService.Services
{
    public class AppData
    {
        private int _number;
        public int Number
        {
            get
            {
                return _number;
            }
            set
            {
                _number = value;
                NotifyDataChanged();
            }
        }

        private string _color;
        public string Color
        {
            get
            {
                return _color;
            }
            set
            {
                _color = value;
                NotifyDataChanged();
            }
        }

        public event Action OnChange;

        private void NotifyDataChanged() => OnChange?.Invoke();
    }
}

Example 3: how to pass property between blazor components

namespace AppDataService.Services
{
    public class AppData
    {
        public int Age { get; set; }
    }
}

Example 4: how to pass property between blazor components

services.AddScoped<Services.AppData>();

Tags:

Misc Example