2013-03-08 65 views
0

如何從密封部分類中的函數返回值?返回類別

我使用像這樣的usercontrols。我有一個用戶控件,調用另一個是列表。當我從這個列表中選擇一行時,我調用SelectionChanged =「RadGrid1_SelectedIndexChanged」,它保存在我想要保存的行的模板類型的變量上。 (直到這裏沒有問題)

當在主頁我試圖訪問該變量,它總是返回我空。 (這裏的問題)

用戶控件:

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

在這裏,我需要的模板值返回炫魅廣東。我怎樣才能做到這一點?

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

如果你不*使用密封的部分類,你會怎麼做? (這些都不影響這個......) – 2013-03-08 04:15:16

+0

我想知道如何從類中返回值。 – 2013-03-08 04:18:27

+0

所以'GroupingZoomedInView'是一個控件,它允許一個控件允許用戶從數據綁定列表中選擇一個模板。在你的MainPage方法中,你實例化一個新的實例並立即嘗試獲取選定的模板。您從未將'gView'放置在任何位置的屏幕上,或允許用戶在嘗試從'gView'中獲取它之前選擇模板。您可能需要引用現有的「gView」,而不是創建新的「gView」。 – 2013-03-09 00:16:49

回答

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 
    } 
} 
+0

這些都不顯示如何從類中返回值! 1)該字段不返回任何內容,2)屬性,而不是類,3)是一種方法。 – 2013-03-08 04:24:58

+0

@丹那更好? – 2013-03-08 04:33:48

+0

我很抱歉,我很不好地解釋自己。我想知道如何從密封的部分類中的函數返回一個方法。我之前嘗試過以前所說的所有這些方法,並且沒有工作之前嘗試訪問模板值。包括,我試圖這樣做:public string Template {get;內部設置; },但它不起作用。你可以幫我嗎? – Kelianosevis 2013-03-08 11:51:03