Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Update blazor component when file changed

I want to update my blazor component when file is changed. This is my code:

<div class="orders">
    <div class="sales">
        <h3>Asks</h3>
        <table class="table text-light orders-table table-hover">
            <thead>
                <tr class="text-center">
                    <th>Order Id</th>
                    <th>Price</th>
                    <th>Quantiy</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @foreach(var order in Model.Sales)
                {
                    <tr>
                        <td class = "">@order.OrderID</td>
                        <td class = "">@order.Price.ToString("0.00")</td>
                        <td class = "">@order.Quantity</td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
    <div class="buys">
        <h3>Bids</h3>
        <table class="table text-light orders-table table-hover">
            <thead>
                <tr class="text-center">
                    <th>Order Id</th>
                    <th>Price</th>
                    <th>Quantiy</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @foreach(var order in Model.Buys)
                {
                    <tr>
                        <td class = "">@order.OrderID</td>
                        <td class = "">@order.Price.ToString("0.00")</td>
                        <td class = "">@order.Quantity</td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
</div>
@code {
    public Orders()
    {
    }

    [Parameter]
    public IndexViewModel Model { get; set; }

    private FileSystemWatcher watcher = new FileSystemWatcher(@"My\folder\directory\");

    protected override void OnInitialized()
    {
        watcher.NotifyFilter = NotifyFilters.Attributes
                                 | NotifyFilters.CreationTime
                                 | NotifyFilters.DirectoryName
                                 | NotifyFilters.FileName
                                 | NotifyFilters.LastAccess
                                 | NotifyFilters.LastWrite
                                 | NotifyFilters.Security
                                 | NotifyFilters.Size;

        watcher.Changed += OnChanged;
        watcher.Created += OnCreated;
        watcher.Deleted += OnDeleted;
        watcher.Renamed += OnRenamed;

        watcher.Filter = "*.txt";
        watcher.IncludeSubdirectories = true;
        watcher.EnableRaisingEvents = true;
    }

    private void OnChanged(object sender, FileSystemEventArgs e)
    {
        Model = new IndexViewModel(new List<OrderViewModel>(), new List<OrderViewModel>());

        Console.WriteLine($"Changed: {e.FullPath}");
        Model.Sales.Add(new OrderViewModel() { OrderID = new Random().Next(int.MaxValue), Price = new Random().Next(10), Quantity =  new Random().Next(100)});
        //Model.Sales.RemoveAt(new Random().Next(Model.Sales.Count - 1));
        Model.Sales = Model.Sales.OrderBy(x => x.Price).ToList();

        Model.Buys.Add(new OrderViewModel() { OrderID = new Random().Next(int.MaxValue), Price = new Random().Next(20), Quantity =  new Random().Next(100)});
        //Model.Buys.RemoveAt(new Random().Next(Model.Sales.Count - 1));
        Model.Buys = Model.Buys.OrderByDescending(x => x.Price).ToList();
    }

    private static void OnCreated(object sender, FileSystemEventArgs e)
    {
        string value = $"Created: {e.FullPath}";
        Console.WriteLine(value);
    }

    private static void OnDeleted(object sender, FileSystemEventArgs e) =>
        Console.WriteLine($"Deleted: {e.FullPath}");

    private static void OnRenamed(object sender, RenamedEventArgs e)
    {
        Console.WriteLine($"Renamed:");
        Console.WriteLine($"    Old: {e.OldFullPath}");
        Console.WriteLine($"    New: {e.FullPath}");
    }
}

When the file is changed method OnChanged is called and IndexViewModel is changed but no changes are made on the screen. I tried making the method static but thus I have to make the IndexViewModel class static too which doesn’t work for me.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try to put a call to the StateHasChanged at the end of the OnChanged method to re-render the UI with the new values:

InvokeAsync( () => StateHasChanged());
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading