본문 바로가기
IT/C#

[c# 교육]day4 2

by dya0 2020. 7. 20.

route

Component

 

client-side blazor

webassembly -> mobile solution andriod Xplatform \watching TV VR-

 

PersonService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BlazorApp4.Data
{
    public class PersonService
    {
        public Task<Person[]> GetPersonAsync()
        {
            List<Person> people = new List<Person>{
                    new Person{ Id =1,Age=11, Gender =true, Name="Son"},
                    new Person{ Id =2,Age=22, Gender =false, Name="Kim"},
                    new Person{ Id =3,Age=33, Gender =true, Name="Lee"},
                    new Person{ Id =4,Age=44, Gender =false, Name="Park"},
                    new Person{ Id =5,Age=55, Gender =true, Name="Ryu"},
                    new Person{ Id =6,Age=66, Gender =true, Name="Jang"},
                    new Person{ Id =7,Age=77, Gender =false, Name="Um"},
                    new Person{ Id =8,Age=83, Gender =true, Name="Oh"},
                    new Person{ Id =9,Age=94, Gender =false, Name="Min"},
                    new Person{ Id =10,Age=15, Gender =true, Name="Song"},
                    new Person{ Id =11,Age=21, Gender =true, Name="An"}
             };
            return Task.FromResult(people.ToArray());
        }


        //2
        public ValueTask<Person[]> GetPerson2Async()
        {
            List<Person> people = new List<Person>
{
    new Person{ Id =1,Age=11, Gender =true, Name="Son"},
    new Person{ Id =2,Age=22, Gender =false, Name="Kim"},
    new Person{ Id =3,Age=33, Gender =true, Name="Lee"},
    new Person{ Id =4,Age=44, Gender =false, Name="Park"},
    new Person{ Id =5,Age=55, Gender =true, Name="Ryu"},
    new Person{ Id =6,Age=66, Gender =true, Name="Jang"},
    new Person{ Id =7,Age=77, Gender =false, Name="Um"}
    };
            return new ValueTask<Person[]>(people.ToArray());
        }






    }
}

WeatherForecase.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BlazorApp4.Data
{
    public class PersonService
    {
        public Task<Person[]> GetPersonAsync()
        {
            List<Person> people = new List<Person>{
                    new Person{ Id =1,Age=11, Gender =true, Name="Son"},
                    new Person{ Id =2,Age=22, Gender =false, Name="Kim"},
                    new Person{ Id =3,Age=33, Gender =true, Name="Lee"},
                    new Person{ Id =4,Age=44, Gender =false, Name="Park"},
                    new Person{ Id =5,Age=55, Gender =true, Name="Ryu"},
                    new Person{ Id =6,Age=66, Gender =true, Name="Jang"},
                    new Person{ Id =7,Age=77, Gender =false, Name="Um"},
                    new Person{ Id =8,Age=83, Gender =true, Name="Oh"},
                    new Person{ Id =9,Age=94, Gender =false, Name="Min"},
                    new Person{ Id =10,Age=15, Gender =true, Name="Song"},
                    new Person{ Id =11,Age=21, Gender =true, Name="An"}
             };
            return Task.FromResult(people.ToArray());
        }


        //2
        public ValueTask<Person[]> GetPerson2Async()
        {
            List<Person> people = new List<Person>
{
    new Person{ Id =1,Age=11, Gender =true, Name="Son"},
    new Person{ Id =2,Age=22, Gender =false, Name="Kim"},
    new Person{ Id =3,Age=33, Gender =true, Name="Lee"},
    new Person{ Id =4,Age=44, Gender =false, Name="Park"},
    new Person{ Id =5,Age=55, Gender =true, Name="Ryu"},
    new Person{ Id =6,Age=66, Gender =true, Name="Jang"},
    new Person{ Id =7,Age=77, Gender =false, Name="Um"}
    };
            return new ValueTask<Person[]>(people.ToArray());
        }
    }
}

WeatherForecastService.cs

using System;
using System.Linq;
using System.Threading.Tasks;

namespace BlazorApp4.Data
{
    public class WeatherForecastService
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        public Task<WeatherForecast[]> GetForecastAsync(DateTime startDate)
        {
            var rng = new Random();
            return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = startDate.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            }).ToArray());
        }
    }
}

Pages  폴더

Helele2Component.razor

@page "/Helele2Component"

@using BlazorApp4.Data
@inject PersonService PerService


<h3>Helele2Component</h3>
<ul>
    @foreach (var kkk in people)
    {
        <li>@kkk.ToString()</li>
    }
</ul>
<hr>
@if (people == null)
{
    <p>없음</p>
}
else
{
    <table class="table">
        <thead>
            <tr>
                <th>Id</th>
                <th>Age. (C)</th>
                <th>Name. (F)</th>
                <th>Gender</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var per in people)
            {
                <tr>
                    <td>@per.Id</td>
                    <td>@per.Age</td>
                    <td>@per.Name</td>
                    <td>@per.Gender</td>
                </tr>
            }
        </tbody>
    </table>
}



@code {
    @* List<Person> people = new List<Person>
        {
            new Person{ Id =1,Age=11, Gender =true, Name="Son"},
            new Person{ Id =2,Age=22, Gender =false, Name="Kim"},
            new Person{ Id =3,Age=33, Gender =true, Name="Lee"},
            new Person{ Id =4,Age=44, Gender =false, Name="Park"},
            new Person{ Id =5,Age=55, Gender =true, Name="Ryu"},
            new Person{ Id =6,Age=66, Gender =true, Name="Jang"},
            new Person{ Id =7,Age=77, Gender =false, Name="Um"},
            new Person{ Id =8,Age=83, Gender =true, Name="Oh"},
            new Person{ Id =9,Age=94, Gender =false, Name="Min"},
            new Person{ Id =10,Age=15, Gender =true, Name="Song"},
            new Person{ Id =11,Age=21, Gender =true, Name="An"}
            };*@
@* injector *@
Person[] people;
@* Injection service*@
protected override async Task OnInitializedAsync()
{
    people = await PerService.GetPersonAsync();

}

}

index.razor

@page "/"

<h1>Hello, world!</h1>

Welcome to your new app.

<SurveyPrompt Title="How is Blazor working for you?" />
<HeleleComponent1/>
<Helele2Component/>
<Component/>

startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using BlazorApp4.Data;

namespace BlazorApp4
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();
            services.AddServerSideBlazor();
            services.AddSingleton<WeatherForecastService>();
            services.AddScoped<PersonService>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapBlazorHub();
                endpoints.MapFallbackToPage("/_Host");
            });
        }
    }
}

 

 

 

결과물

 

 

 

configure서비스랑 configuration 알것

'IT > C#' 카테고리의 다른 글

[.Net교육] Day 6 설치과정  (0) 2020.07.22
[.NET 교육] Day5 오전  (0) 2020.07.21
[C#] 상속  (0) 2020.07.10
[C#] C# 클래스 확장  (0) 2020.07.09
[C#] 다형성  (0) 2020.07.08