从类中返回值

时间:2013-03-08 04:11:26

标签: c# partial-classes sealed

如何从密封的部分类中的函数返回值?

我使用像这样的usercontrols。我有一个usercontrol调用另一个列表。当我从这个列表中选择一行时,我调用SelectionChanged =“RadGrid1_SelectedIndexChanged”,它将模板类型的变量保存到我想要保存的行。 (直到这里没有问题)

当我在主页中尝试访问该变量时,它总是返回null。 (问题在这里)

用户控件:

<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;
    }
}

这里我需要将模板值返回给MainPage。我怎么能这样做?

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; }
 }

1 个答案:

答案 0 :(得分:4)

您无法从类中“返回”某个值。这仅适用于功能。但是,您可以通过多种方式访问​​类中的数据。

使用公共字段(有效,但做法不好)

public string template;

使用公共财产(更好)

public string Template { get; set; }

使用公开方法

public string GetTemplate()
{
    return this.template;
}

对于此处列出的任何技术,在MainPage课程中,您只能从方法正文中访问模板值。

public class MainPage
{
    private GroupingZoomedInView gView;

    public void SomeMethod()
    {
        String temp = gView.GetTemplate();
    }
}
顺便说一下,任何一种课程都是如此。这一个sealedpartial这一事实没有任何区别。


<强>更新

在评论中,您说您想从函数返回方法。在这种情况下,您需要执行的操作使用上述任何技术,但将返回类型从string更改为Func<string>(或某些自定义委托类型,我不会在此处介绍)。

来自会员方法

private string GetTemplate() 
{
    return this.template;
}

public Func<string> GetTemplateMethod()
{
    return new Func<string>(this.GetTemplate);
}

来自lambda表达式

private string template;

public Func<string> GetTemplateMethod()
{
    return () => this.template;
}

MainPage课程中,您可以使用GetTemplateMethod这两种技巧。

public class MainPage
{
    private GroupingZoomedInView gView;

    public void SomeMethod()
    {
        Func<string> method = gView.GetTemplateMethod();
        string temp = method(); // executes the method
    }
}