0

如何从 Sealed Partial 类中的 aa 函数返回值?

我使用这样的用户控件。我有一个用户控件,它调用另一个列表。当我从该列表中选择一行时,我调用 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>

分组ZoomedInView.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; }
 }
4

1 回答 1

3

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

使用公共领域(有效,但不好的做法)

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没有区别。


更新

在评论中,您说您想从函数中返回一个方法。在这种情况下,您需要使用上述任何技术,但将返回类型从stringto更改为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
    }
}
于 2013-03-08T04:12:40.510 回答