From 1ea583ecd0fd7e8bae5526a26a0a13cefcc70804 Mon Sep 17 00:00:00 2001 From: Lucie Bedouret Date: Fri, 18 Nov 2022 15:24:50 +0100 Subject: [PATCH] Continue : to Using the component but an exception needs to be fixed --- .../myBlazorApp/Components/Crafting.razor | 51 +++++++ .../myBlazorApp/Components/Crafting.razor.cs | 80 +++++++++++ .../myBlazorApp/Components/Crafting.razor.css | 19 +++ .../myBlazorApp/Components/Crafting.razor.js | 16 +++ .../myBlazorApp/Components/CraftingAction.cs | 13 ++ .../myBlazorApp/Components/CraftingItem.razor | 14 ++ .../Components/CraftingItem.razor.cs | 66 +++++++++ .../Components/CraftingItem.razor.css | 6 + .../myBlazorApp/Components/CraftingRecipe.cs | 12 ++ myBlazorApp/myBlazorApp/Pages/Index.razor | 9 +- myBlazorApp/myBlazorApp/Pages/Index.razor.cs | 34 +++++ myBlazorApp/myBlazorApp/Pages/List.razor | 2 +- myBlazorApp/myBlazorApp/Pages/List.razor.cs | 4 + myBlazorApp/myBlazorApp/Pages/_Layout.cshtml | 1 + .../Properties/launchSettings.json | 2 +- .../Resources/Pages.List.Designer.cs | 9 ++ .../Resources/Pages.List.fr-FR.Designer.cs | 69 +++++++++ .../Resources/Pages.List.fr-FR.resx | 124 ++++++++++++++++ .../myBlazorApp/Resources/Pages.List.resx | 133 ++++++++++++++++-- .../myBlazorApp/Services/DataLocalService.cs | 20 +++ .../myBlazorApp/Services/IDataService.cs | 2 + myBlazorApp/myBlazorApp/myBlazorApp.csproj | 9 ++ 22 files changed, 678 insertions(+), 17 deletions(-) create mode 100644 myBlazorApp/myBlazorApp/Components/Crafting.razor create mode 100644 myBlazorApp/myBlazorApp/Components/Crafting.razor.cs create mode 100644 myBlazorApp/myBlazorApp/Components/Crafting.razor.css create mode 100644 myBlazorApp/myBlazorApp/Components/Crafting.razor.js create mode 100644 myBlazorApp/myBlazorApp/Components/CraftingAction.cs create mode 100644 myBlazorApp/myBlazorApp/Components/CraftingItem.razor create mode 100644 myBlazorApp/myBlazorApp/Components/CraftingItem.razor.cs create mode 100644 myBlazorApp/myBlazorApp/Components/CraftingItem.razor.css create mode 100644 myBlazorApp/myBlazorApp/Components/CraftingRecipe.cs create mode 100644 myBlazorApp/myBlazorApp/Pages/Index.razor.cs create mode 100644 myBlazorApp/myBlazorApp/Resources/Pages.List.fr-FR.Designer.cs create mode 100644 myBlazorApp/myBlazorApp/Resources/Pages.List.fr-FR.resx diff --git a/myBlazorApp/myBlazorApp/Components/Crafting.razor b/myBlazorApp/myBlazorApp/Components/Crafting.razor new file mode 100644 index 0000000..5c948cb --- /dev/null +++ b/myBlazorApp/myBlazorApp/Components/Crafting.razor @@ -0,0 +1,51 @@ + +
+
+
+ +
Available items:
+
+
+ + @foreach (var item in Items) + { + + } +
+
+ +
+ +
+
Recipe
+ +
+ +
+ + + + + + + + + +
+
+ +
Result
+
+ +
+
+ +
+
Actions
+
+ +
+
+
+
+
diff --git a/myBlazorApp/myBlazorApp/Components/Crafting.razor.cs b/myBlazorApp/myBlazorApp/Components/Crafting.razor.cs new file mode 100644 index 0000000..4b76577 --- /dev/null +++ b/myBlazorApp/myBlazorApp/Components/Crafting.razor.cs @@ -0,0 +1,80 @@ +using Microsoft.AspNetCore.Components; +using Microsoft.JSInterop; +using myBlazorApp.Models; +using System.Collections.ObjectModel; +using System.Collections.Specialized; + +namespace myBlazorApp.Components +{ + public partial class Crafting + { + private Item _recipeResult; + + public Crafting() + { + Actions = new ObservableCollection(); + Actions.CollectionChanged += OnActionsCollectionChanged; + this.RecipeItems = new List { null, null, null, null, null, null, null, null, null }; + } + + public ObservableCollection Actions { get; set; } + public Item CurrentDragItem { get; set; } + + [Parameter] + public List Items { get; set; } + + public List RecipeItems { get; set; } + + public Item RecipeResult + { + get => this._recipeResult; + set + { + if (this._recipeResult == value) + { + return; + } + + this._recipeResult = value; + this.StateHasChanged(); + } + } + + [Parameter] + public List Recipes { get; set; } + + /// + /// Gets or sets the java script runtime. + /// + [Inject] + internal IJSRuntime JavaScriptRuntime { get; set; } + + public void CheckRecipe() + { + RecipeResult = null; + + // Get the current model + var currentModel = string.Join("|", this.RecipeItems.Select(s => s != null ? s.Name : string.Empty)); + + this.Actions.Add(new CraftingAction { Action = $"Items : {currentModel}" }); + + foreach (var craftingRecipe in Recipes) + { + // Get the recipe model + var recipeModel = string.Join("|", craftingRecipe.Have.SelectMany(s => s)); + + this.Actions.Add(new CraftingAction { Action = $"Recipe model : {recipeModel}" }); + + if (currentModel == recipeModel) + { + RecipeResult = craftingRecipe.Give; + } + } + } + + private void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) + { + JavaScriptRuntime.InvokeVoidAsync("Crafting.AddActions", e.NewItems); + } + } +} \ No newline at end of file diff --git a/myBlazorApp/myBlazorApp/Components/Crafting.razor.css b/myBlazorApp/myBlazorApp/Components/Crafting.razor.css new file mode 100644 index 0000000..2a388f2 --- /dev/null +++ b/myBlazorApp/myBlazorApp/Components/Crafting.razor.css @@ -0,0 +1,19 @@ +.css-grid { + grid-template-columns: repeat(4,minmax(0,1fr)); + gap: 10px; + display: grid; + width: 286px; +} + +.css-recipe { + grid-template-columns: repeat(3,minmax(0,1fr)); + gap: 10px; + display: grid; + width: 212px; +} + +.actions { + border: 1px solid black; + height: 250px; + overflow: scroll; +} diff --git a/myBlazorApp/myBlazorApp/Components/Crafting.razor.js b/myBlazorApp/myBlazorApp/Components/Crafting.razor.js new file mode 100644 index 0000000..8fdb58e --- /dev/null +++ b/myBlazorApp/myBlazorApp/Components/Crafting.razor.js @@ -0,0 +1,16 @@ +window.Crafting = +{ + AddActions: function (data) { + + data.forEach(element => { + var div = document.createElement('div'); + div.innerHTML = 'Action: ' + element.action + ' - Index: ' + element.index; + + if (element.item) { + div.innerHTML += ' - Item Name: ' + element.item.name; + } + + document.getElementById('actions').appendChild(div); + }); + } +} \ No newline at end of file diff --git a/myBlazorApp/myBlazorApp/Components/CraftingAction.cs b/myBlazorApp/myBlazorApp/Components/CraftingAction.cs new file mode 100644 index 0000000..828a544 --- /dev/null +++ b/myBlazorApp/myBlazorApp/Components/CraftingAction.cs @@ -0,0 +1,13 @@ +using System; +using myBlazorApp.Models; + +namespace myBlazorApp.Components +{ + public class CraftingAction + { + public string Action { get; set; } + public int Index { get; set; } + public Item Item { get; set; } + } +} + diff --git a/myBlazorApp/myBlazorApp/Components/CraftingItem.razor b/myBlazorApp/myBlazorApp/Components/CraftingItem.razor new file mode 100644 index 0000000..e9b5cac --- /dev/null +++ b/myBlazorApp/myBlazorApp/Components/CraftingItem.razor @@ -0,0 +1,14 @@ +
diff --git a/myBlazorApp/myBlazorApp/Components/CraftingItem.razor.cs b/myBlazorApp/myBlazorApp/Components/CraftingItem.razor.cs new file mode 100644 index 0000000..1fd0aae --- /dev/null +++ b/myBlazorApp/myBlazorApp/Components/CraftingItem.razor.cs @@ -0,0 +1,66 @@ +using System; +using Blazorise; +using Microsoft.AspNetCore.Components; +using myBlazorApp.Models; + +namespace myBlazorApp.Components +{ + public partial class CraftingItem + { + [Parameter] + public int Index { get; set; } + + [Parameter] + public Item Item { get; set; } + + [Parameter] + public bool NoDrop { get; set; } + + [CascadingParameter] + public Crafting Parent { get; set; } + + internal void OnDragEnter() + { + if (NoDrop) + { + return; + } + + Parent.Actions.Add(new CraftingAction { Action = "Drag Enter", Item = this.Item, Index = this.Index }); + } + + internal void OnDragLeave() + { + if (NoDrop) + { + return; + } + + Parent.Actions.Add(new CraftingAction { Action = "Drag Leave", Item = this.Item, Index = this.Index }); + } + + internal void OnDrop() + { + if (NoDrop) + { + return; + } + + this.Item = Parent.CurrentDragItem; + Parent.RecipeItems[this.Index] = this.Item; + + Parent.Actions.Add(new CraftingAction { Action = "Drop", Item = this.Item, Index = this.Index }); + + // Check recipe + Parent.CheckRecipe(); + } + + private void OnDragStart() + { + Parent.CurrentDragItem = this.Item; + + Parent.Actions.Add(new CraftingAction { Action = "Drag Start", Item = this.Item, Index = this.Index }); + } + } +} + diff --git a/myBlazorApp/myBlazorApp/Components/CraftingItem.razor.css b/myBlazorApp/myBlazorApp/Components/CraftingItem.razor.css new file mode 100644 index 0000000..b2d4521 --- /dev/null +++ b/myBlazorApp/myBlazorApp/Components/CraftingItem.razor.css @@ -0,0 +1,6 @@ +.item { + width: 64px; + height: 64px; + border: 1px solid; + overflow: hidden; +} diff --git a/myBlazorApp/myBlazorApp/Components/CraftingRecipe.cs b/myBlazorApp/myBlazorApp/Components/CraftingRecipe.cs new file mode 100644 index 0000000..077f54f --- /dev/null +++ b/myBlazorApp/myBlazorApp/Components/CraftingRecipe.cs @@ -0,0 +1,12 @@ +using System; +using myBlazorApp.Models; + +namespace myBlazorApp.Components +{ + public class CraftingRecipe + { + public Item Give { get; set; } + public List> Have{ get; set; } + } +} + diff --git a/myBlazorApp/myBlazorApp/Pages/Index.razor b/myBlazorApp/myBlazorApp/Pages/Index.razor index aa25222..8af0e6e 100644 --- a/myBlazorApp/myBlazorApp/Pages/Index.razor +++ b/myBlazorApp/myBlazorApp/Pages/Index.razor @@ -1,5 +1,5 @@ @using System.Globalization - +@using myBlazorApp.Components @page "/" Index @@ -10,7 +10,10 @@ Welcome to your new app. -

CurrentCulture: @CultureInfo.CurrentCulture -

\ No newline at end of file +

+ +
+ +
\ No newline at end of file diff --git a/myBlazorApp/myBlazorApp/Pages/Index.razor.cs b/myBlazorApp/myBlazorApp/Pages/Index.razor.cs new file mode 100644 index 0000000..9eb62fe --- /dev/null +++ b/myBlazorApp/myBlazorApp/Pages/Index.razor.cs @@ -0,0 +1,34 @@ +using System; +using Microsoft.AspNetCore.Components; +using myBlazorApp.Components; +using myBlazorApp.Models; +using myBlazorApp.Services; + +namespace myBlazorApp.Pages +{ + public partial class Index + { + [Inject] + public IDataService DataService { get; set; } + + public List Items { get; set; } = new List(); + + private List Recipes { get; set; } = new List(); + + protected override async Task OnAfterRenderAsync(bool firstRender) + { + base.OnAfterRenderAsync(firstRender); + + if (!firstRender) + { + return; + } + + Items = await DataService.List(0, await DataService.Count()); + Recipes = await DataService.GetRecipes(); + + StateHasChanged(); + } + } +} + diff --git a/myBlazorApp/myBlazorApp/Pages/List.razor b/myBlazorApp/myBlazorApp/Pages/List.razor index 21823bc..cb159e4 100644 --- a/myBlazorApp/myBlazorApp/Pages/List.razor +++ b/myBlazorApp/myBlazorApp/Pages/List.razor @@ -2,7 +2,7 @@ @using myBlazorApp.Models; -

List

+

@Localizer["Title"]

diff --git a/myBlazorApp/myBlazorApp/Pages/List.razor.cs b/myBlazorApp/myBlazorApp/Pages/List.razor.cs index 79530a7..cafbca1 100644 --- a/myBlazorApp/myBlazorApp/Pages/List.razor.cs +++ b/myBlazorApp/myBlazorApp/Pages/List.razor.cs @@ -4,6 +4,7 @@ using Blazored.Modal; using Blazored.Modal.Services; using Blazorise.DataGrid; using Microsoft.AspNetCore.Components; +using Microsoft.Extensions.Localization; using myBlazorApp.Modals; using myBlazorApp.Models; using myBlazorApp.Services; @@ -12,6 +13,9 @@ namespace myBlazorApp.Pages { public partial class List { + [Inject] + public IStringLocalizer Localizer { get; set; } + private List items; private int totalItem; diff --git a/myBlazorApp/myBlazorApp/Pages/_Layout.cshtml b/myBlazorApp/myBlazorApp/Pages/_Layout.cshtml index 7e0555a..a400691 100644 --- a/myBlazorApp/myBlazorApp/Pages/_Layout.cshtml +++ b/myBlazorApp/myBlazorApp/Pages/_Layout.cshtml @@ -35,6 +35,7 @@ + diff --git a/myBlazorApp/myBlazorApp/Properties/launchSettings.json b/myBlazorApp/myBlazorApp/Properties/launchSettings.json index c849ccb..fd32e8f 100644 --- a/myBlazorApp/myBlazorApp/Properties/launchSettings.json +++ b/myBlazorApp/myBlazorApp/Properties/launchSettings.json @@ -11,7 +11,7 @@ "myBlazorApp": { "commandName": "Project", "launchBrowser": true, - "applicationUrl": "https://localhost:7064;http://localhost:5068", + "applicationUrl": "https://localhost:5065;http://localhost:6789", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/myBlazorApp/myBlazorApp/Resources/Pages.List.Designer.cs b/myBlazorApp/myBlazorApp/Resources/Pages.List.Designer.cs index db1a6f7..cd58bdd 100644 --- a/myBlazorApp/myBlazorApp/Resources/Pages.List.Designer.cs +++ b/myBlazorApp/myBlazorApp/Resources/Pages.List.Designer.cs @@ -56,5 +56,14 @@ namespace myBlazorApp.Resources { resourceCulture = value; } } + + /// + /// Looks up a localized string similar to List of items. + /// + internal static string Title { + get { + return ResourceManager.GetString("Title", resourceCulture); + } + } } } diff --git a/myBlazorApp/myBlazorApp/Resources/Pages.List.fr-FR.Designer.cs b/myBlazorApp/myBlazorApp/Resources/Pages.List.fr-FR.Designer.cs new file mode 100644 index 0000000..8bfcca7 --- /dev/null +++ b/myBlazorApp/myBlazorApp/Resources/Pages.List.fr-FR.Designer.cs @@ -0,0 +1,69 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace myBlazorApp.Resources { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// This class was generated by MSBuild using the GenerateResource task. + /// To add or remove a member, edit your .resx file then rerun MSBuild. + /// + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Build.Tasks.StronglyTypedResourceBuilder", "15.1.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Pages_List_fr_FR { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Pages_List_fr_FR() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("myBlazorApp.Resources.Pages.List.fr-FR", typeof(Pages_List_fr_FR).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to Liste d'Éléments. + /// + internal static string Title { + get { + return ResourceManager.GetString("Title", resourceCulture); + } + } + } +} diff --git a/myBlazorApp/myBlazorApp/Resources/Pages.List.fr-FR.resx b/myBlazorApp/myBlazorApp/Resources/Pages.List.fr-FR.resx new file mode 100644 index 0000000..c953b95 --- /dev/null +++ b/myBlazorApp/myBlazorApp/Resources/Pages.List.fr-FR.resx @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Liste d'Éléments + + + \ No newline at end of file diff --git a/myBlazorApp/myBlazorApp/Resources/Pages.List.resx b/myBlazorApp/myBlazorApp/Resources/Pages.List.resx index 70c400c..96e7209 100644 --- a/myBlazorApp/myBlazorApp/Resources/Pages.List.resx +++ b/myBlazorApp/myBlazorApp/Resources/Pages.List.resx @@ -1,15 +1,124 @@  - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + List of items + + \ No newline at end of file diff --git a/myBlazorApp/myBlazorApp/Services/DataLocalService.cs b/myBlazorApp/myBlazorApp/Services/DataLocalService.cs index 3cc6bdc..c5360e8 100644 --- a/myBlazorApp/myBlazorApp/Services/DataLocalService.cs +++ b/myBlazorApp/myBlazorApp/Services/DataLocalService.cs @@ -1,6 +1,7 @@  using System; using Blazored.LocalStorage; using Microsoft.AspNetCore.Components; +using myBlazorApp.Components; using myBlazorApp.Factories; using myBlazorApp.Models; @@ -161,5 +162,24 @@ namespace myBlazorApp.Services // Save the data await _localStorage.SetItemAsync("data", currentData); } + + public Task> GetRecipes() + { + var items = new List + { + new CraftingRecipe + { + Give = new Item { DisplayName = "Diamond", Name = "diamond" }, + Have = new List> + { + new List { "dirt", "dirt", "dirt" }, + new List { "dirt", null, "dirt" }, + new List { "dirt", "dirt", "dirt" } + } + } + }; + + return Task.FromResult(items); + } } } diff --git a/myBlazorApp/myBlazorApp/Services/IDataService.cs b/myBlazorApp/myBlazorApp/Services/IDataService.cs index 0b245cb..845bcee 100644 --- a/myBlazorApp/myBlazorApp/Services/IDataService.cs +++ b/myBlazorApp/myBlazorApp/Services/IDataService.cs @@ -1,4 +1,5 @@ using System; +using myBlazorApp.Components; using myBlazorApp.Models; namespace myBlazorApp.Services @@ -11,6 +12,7 @@ namespace myBlazorApp.Services Task GetById(int id); Task Update(int id, ItemModel model); Task Delete(int id); + Task> GetRecipes(); } } diff --git a/myBlazorApp/myBlazorApp/myBlazorApp.csproj b/myBlazorApp/myBlazorApp/myBlazorApp.csproj index f39f62b..47ace7b 100644 --- a/myBlazorApp/myBlazorApp/myBlazorApp.csproj +++ b/myBlazorApp/myBlazorApp/myBlazorApp.csproj @@ -20,6 +20,7 @@ + @@ -29,6 +30,7 @@ + @@ -43,10 +45,17 @@ ResXFileCodeGenerator Pages.List.Designer.cs + + ResXFileCodeGenerator + Pages.List.fr-FR.Designer.cs + Pages.List.resx + + Pages.List.fr-FR.resx +