Skip to content

Commit

Permalink
Merge pull request #1 from AnanthaLakshmiKannan/master
Browse files Browse the repository at this point in the history
Added KB for How to access a named ListView inside a XAML DataTemplate in .NET MAUI (SfListView)?
  • Loading branch information
PiruthivirajM authored Jan 3, 2024
2 parents 6602b52 + 325160a commit 02274ba
Show file tree
Hide file tree
Showing 42 changed files with 1,303 additions and 0 deletions.
14 changes: 14 additions & 0 deletions App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:ListViewMaui"
x:Class="ListViewMaui.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
12 changes: 12 additions & 0 deletions App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace ListViewMaui
{
public partial class App : Application
{
public App()
{
InitializeComponent();

MainPage = new MainPage();
}
}
}
85 changes: 85 additions & 0 deletions Helper/Behavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using Syncfusion.Maui.ListView;
using Syncfusion.Maui.Popup;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ListViewMaui
{
public class GridBehavior : Behavior<Grid>
{
Grid grid;
SfListView listView;
Button button;
protected override void OnAttachedTo(BindableObject bindable)
{
grid = bindable as Grid;
grid.ChildAdded += Grid_ChildAdded;
}
//Method 1 : Get SfListView reference using Grid.ChildAdded Event
private void Grid_ChildAdded(object sender, ElementEventArgs e)
{
if (e.Element is SfListView)
{
listView = e.Element as SfListView;
listView.RefreshView();
}
if (e.Element is Button)
{
button = e.Element as Button;
button.Clicked += Button_Clicked;
}
}
//Method 2 : Get SfListView reference using FindByName
private void Button_Clicked(object sender, EventArgs e)
{
listView = grid.FindByName<SfListView>("listView");
App.Current.MainPage.DisplayAlert("Information", "ListView instance obtained", "Ok");
listView.ItemTapped += ListView_ItemTapped;
}

private void ListView_ItemTapped(object sender, Syncfusion.Maui.ListView.ItemTappedEventArgs e)
{
App.Current.MainPage.DisplayAlert("Information", "ListView ItemTapped", "Ok");
}

protected override void OnDetachingFrom(BindableObject bindable)
{
button.Clicked -= Button_Clicked;
grid.ChildAdded -= Grid_ChildAdded;
listView.ItemTapped -= ListView_ItemTapped;
listView = null;
button = null;
grid = null;
base.OnDetachingFrom(bindable);
}
}

public class ContentPageBehavior : Behavior<ContentPage>
{
ContentPage page;
SfPopup popupLayout;
Button button;
protected override void OnAttachedTo(ContentPage bindable)
{
//page = bindable;
popupLayout = bindable.FindByName<SfPopup>("popupLayout");
button = bindable.FindByName<Button>("ShowPopup");
button.Clicked += Button_Clicked;
base.OnAttachedTo(bindable);
}

private void Button_Clicked(object sender, EventArgs e)
{
popupLayout.Show();
}

protected override void OnDetachingFrom(ContentPage bindable)
{
button.Clicked -= Button_Clicked;
base.OnDetachingFrom(bindable);
}
}
}
Binary file added Images/image_a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Images/image_b.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Images/image_c.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Images/image_d.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Images/image_e.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
83 changes: 83 additions & 0 deletions ListViewMaui.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks>
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
<!-- <TargetFrameworks>$(TargetFrameworks);net8.0-tizen</TargetFrameworks> -->

<!-- Note for MacCatalyst:
The default runtime is maccatalyst-x64, except in Release config, in which case the default is maccatalyst-x64;maccatalyst-arm64.
When specifying both architectures, use the plural <RuntimeIdentifiers> instead of the singular <RuntimeIdentifier>.
The Mac App Store will NOT accept apps with ONLY maccatalyst-arm64 indicated;
either BOTH runtimes must be indicated or ONLY macatalyst-x64. -->
<!-- For example: <RuntimeIdentifiers>maccatalyst-x64;maccatalyst-arm64</RuntimeIdentifiers> -->

<OutputType>Exe</OutputType>
<RootNamespace>ListViewMaui</RootNamespace>
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<!-- Display name -->
<ApplicationTitle>ListViewMaui</ApplicationTitle>

<!-- App Identifier -->
<ApplicationId>com.companyname.listviewmaui</ApplicationId>

<!-- Versions -->
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
<ApplicationVersion>1</ApplicationVersion>

<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">11.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">13.1</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
</PropertyGroup>

<ItemGroup>
<!-- App Icon -->
<MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />

<!-- Splash Screen -->
<MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />

<!-- Images -->
<MauiImage Include="Resources\Images\*" />
<MauiImage Update="Resources\Images\dotnet_bot.png" Resize="True" BaseSize="300,185" />

<!-- Custom Fonts -->
<MauiFont Include="Resources\Fonts\*" />

<!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup>

<ItemGroup>
<None Remove="Images\image_a.png" />
<None Remove="Images\image_b.png" />
<None Remove="Images\image_c.png" />
<None Remove="Images\image_d.png" />
<None Remove="Images\image_e.png" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
<PackageReference Include="Syncfusion.Maui.ListView" Version="*" />
<PackageReference Include="Syncfusion.Maui.Popup" Version="*" />
</ItemGroup>

<ItemGroup>
<MauiImage Include="Images\image_a.png" />
<MauiImage Include="Images\image_b.png" />
<MauiImage Include="Images\image_c.png" />
<MauiImage Include="Images\image_d.png" />
<MauiImage Include="Images\image_e.png" />
</ItemGroup>

</Project>
27 changes: 27 additions & 0 deletions ListViewMaui.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34330.188
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ListViewMaui", "ListViewMaui.csproj", "{3057B52E-49F9-4762-A642-5F81DCEBC106}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3057B52E-49F9-4762-A642-5F81DCEBC106}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3057B52E-49F9-4762-A642-5F81DCEBC106}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3057B52E-49F9-4762-A642-5F81DCEBC106}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{3057B52E-49F9-4762-A642-5F81DCEBC106}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3057B52E-49F9-4762-A642-5F81DCEBC106}.Release|Any CPU.Build.0 = Release|Any CPU
{3057B52E-49F9-4762-A642-5F81DCEBC106}.Release|Any CPU.Deploy.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {325E1898-6963-491C-A60C-C15C777F6084}
EndGlobalSection
EndGlobal
27 changes: 27 additions & 0 deletions MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.Extensions.Logging;
using Syncfusion.Maui.Core.Hosting;

namespace ListViewMaui
{
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.ConfigureSyncfusionCore()
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});

#if DEBUG
builder.Logging.AddDebug();
#endif

return builder.Build();
}
}
}
73 changes: 73 additions & 0 deletions Model/Contacts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ListViewMaui
{
public class Contacts : INotifyPropertyChanged
{
private string contactName;
private string contactNumber;
private ImageSource image;
private ImageSource contactType;

public Contacts(string name, string number)
{
contactName = name;
contactNumber = number;
}

public Contacts()
{
}

public string ContactName
{
get { return contactName; }
set
{
if (contactName != value)
{
contactName = value;
this.RaisedOnPropertyChanged("ContactName");
}
}
}

public string ContactNumber
{
get { return contactNumber; }
set
{
if (contactNumber != value)
{
contactNumber = value;
this.RaisedOnPropertyChanged("ContactNumber");
}
}
}

public ImageSource ContactImage
{
get { return this.image; }
set
{
this.image = value;
this.RaisedOnPropertyChanged("ContactImage");
}
}

public event PropertyChangedEventHandler PropertyChanged;

public void RaisedOnPropertyChanged(string _PropertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(_PropertyName));
}
}
}
}
6 changes: 6 additions & 0 deletions Platforms/Android/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
11 changes: 11 additions & 0 deletions Platforms/Android/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Android.App;
using Android.Content.PM;
using Android.OS;

namespace ListViewMaui
{
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
{
}
}
16 changes: 16 additions & 0 deletions Platforms/Android/MainApplication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Android.App;
using Android.Runtime;

namespace ListViewMaui
{
[Application]
public class MainApplication : MauiApplication
{
public MainApplication(IntPtr handle, JniHandleOwnership ownership)
: base(handle, ownership)
{
}

protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
}
6 changes: 6 additions & 0 deletions Platforms/Android/Resources/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#512BD4</color>
<color name="colorPrimaryDark">#2B0B98</color>
<color name="colorAccent">#2B0B98</color>
</resources>
10 changes: 10 additions & 0 deletions Platforms/MacCatalyst/AppDelegate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Foundation;

namespace ListViewMaui
{
[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
}
14 changes: 14 additions & 0 deletions Platforms/MacCatalyst/Entitlements.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<!-- See https://aka.ms/maui-publish-app-store#add-entitlements for more information about adding entitlements.-->
<dict>
<!-- App Sandbox must be enabled to distribute a MacCatalyst app through the Mac App Store. -->
<key>com.apple.security.app-sandbox</key>
<true/>
<!-- When App Sandbox is enabled, this value is required to open outgoing network connections. -->
<key>com.apple.security.network.client</key>
<true/>
</dict>
</plist>

Loading

0 comments on commit 02274ba

Please sign in to comment.