Reputation: 65
How can i return a value from a a function in a Sealed Partial class?
I use the usercontrols like this. I have a usercontrol that calls another one that is a list. When i selected a row from this list, i call SelectionChanged="RadGrid1_SelectedIndexChanged" and it saves on a variable of type Templates the row i want to save. (Until here there is no problem)
When in the mainpage i try to access to that variable, it always returns me null. (Problem here)
UserControls:
<UserControl
x:Class="Stuff.Grouping.TableControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Stuff.Grouping"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid>
<SemanticZoom ScrollViewer.HorizontalScrollMode="Disabled" ScrollViewer.VerticalScrollMode="Disabled">
<SemanticZoom.ZoomedInView>
<local:GroupingZoomedInView Margin="0 0 30 50"/>
</SemanticZoom.ZoomedInView>
<SemanticZoom.ZoomedOutView>
<GridView Margin="30 30 30 50">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Horizontal" MaximumRowsOrColumns="7"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.ItemTemplate>
<DataTemplate>
<Border Background="#FF3399FF" MinWidth="100" MinHeight="100">
<TextBlock Text="{Binding}" FontSize="60" Margin="10"/>
</Border>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</SemanticZoom.ZoomedOutView>
</SemanticZoom>
</Grid>
</UserControl>
GroupingZoomedInView.xaml
<UserControl
x:Class="Stuff.Grouping.GroupingZoomedInView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Stuff.Grouping.Data"
xmlns:telerikGrid="using:Telerik.UI.Xaml.Controls.Grid"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid>
<Grid.Resources>
<local:PeopleViewModel x:Key="Model"/>
</Grid.Resources>
<telerikGrid:RadDataGrid x:Name="dataGrid" ItemsSource="{Binding Data,Source={StaticResource Model}}" AutoGenerateColumns="False" FontSize="{StaticResource ControlContentThemeFontSize}" SelectionChanged="RadGrid1_SelectedIndexChanged">
<telerikGrid:RadDataGrid.GroupDescriptors>
<telerikGrid:DelegateGroupDescriptor>
<telerikGrid:DelegateGroupDescriptor.KeyLookup>
<local:AlpabeticGroupKeyLookup/>
</telerikGrid:DelegateGroupDescriptor.KeyLookup>
</telerikGrid:DelegateGroupDescriptor>
</telerikGrid:RadDataGrid.GroupDescriptors>
<telerikGrid:RadDataGrid.Columns>
<telerikGrid:DataGridTextColumn PropertyName="Template"/>
<telerikGrid:DataGridTextColumn PropertyName="data"/>
<telerikGrid:DataGridTextColumn PropertyName="info"/>
<telerikGrid:DataGridTextColumn PropertyName="score"/>
<telerikGrid:DataGridTextColumn PropertyName="result"/>
<telerikGrid:DataGridTextColumn PropertyName="repeats"/>
</telerikGrid:RadDataGrid.Columns>
</telerikGrid:RadDataGrid>
</Grid>
</UserControl>
GroupingZoomedInView.xaml.cs
public sealed partial class GroupingZoomedInView : UserControl, ISemanticZoomInformation
{
public void RadGrid1_SelectedIndexChanged(object sender, DataGridSelectionChangedEventArgs e)
{
template = (Templates)dataGrid.SelectedItem;
}
public void StartViewChangeFrom(SemanticZoomLocation source, SemanticZoomLocation destination)
{
source.Item = this.dataGrid.GetDataView().Items.OfType<IDataGroup>().Select(c => c.Key);
}
public void StartViewChangeTo(SemanticZoomLocation source, SemanticZoomLocation destination)
{
var dataview = this.dataGrid.GetDataView();
var group = dataview.Items.OfType<IDataGroup>().Where(c => c.Key.Equals(source.Item)).FirstOrDefault();
var lastGroup = dataview.Items.Last() as IDataGroup;
if (group != null && lastGroup != null)
{
this.dataGrid.ScrollItemIntoView(lastGroup.ChildItems[lastGroup.ChildItems.Count - 1], () =>
{
this.dataGrid.ScrollItemIntoView(group.ChildItems[0]);
});
}
}
public Func<Templates> GetTemplateMethod()
{
return () => this.template;
}
}
Here i need to return the template value to MainPage. How can i do that?
public MainPage()
{
GroupingZoomedInView gView = new GroupingZoomedInView();
Func<Templates> method = gView.GetTemplateMethod();
Templates temp = method();
}
public class Templates(){
public String filename { get; set; }
public String data { get; set; }
}
Upvotes: 0
Views: 2690
Reputation: 149050
You cannot "return" a value from a class. That only applies to functions. However, you can access the data within a class in several ways.
Use a public field (works, but bad practice)
public string template;
Use a public property (better)
public string Template { get; set; }
Use a public method
public string GetTemplate()
{
return this.template;
}
For any technique listed here, in your MainPage
class you will only be able to access the template value from within the body of a method.
public class MainPage
{
private GroupingZoomedInView gView;
public void SomeMethod()
{
String temp = gView.GetTemplate();
}
}
BTW, the same goes for any kind of class. The fact that this one is sealed
or partial
makes no difference.
Update
In a comment, you said you wanted to return a method from a function. In that case, what you need to do use any of the above techniques, but change the return type from string
to Func<string>
(or some custom delegate type, which I won't go into here).
From member method
private string GetTemplate()
{
return this.template;
}
public Func<string> GetTemplateMethod()
{
return new Func<string>(this.GetTemplate);
}
From a lambda expression
private string template;
public Func<string> GetTemplateMethod()
{
return () => this.template;
}
From either of these techniques, in your MainPage
class you can use the GetTemplateMethod
like this.
public class MainPage
{
private GroupingZoomedInView gView;
public void SomeMethod()
{
Func<string> method = gView.GetTemplateMethod();
string temp = method(); // executes the method
}
}
Upvotes: 3